debugger.cc revision c901dd7bdc80b953d04100ef2f54b8d1ca5f466b
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.
647934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstromstatic const size_t kDefaultNumAllocRecords = 64*1024;  // Must be a power of 2.
65475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
66b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchiclass AllocRecordStackTraceElement {
67b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi public:
68b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  AllocRecordStackTraceElement() : method_(nullptr), dex_pc_(0) {
69b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
70545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
71b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  int32_t LineNumber() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
72b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    mirror::ArtMethod* method = Method();
73b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    DCHECK(method != nullptr);
74b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    return method->GetLineNumFromDexPC(DexPc());
75412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
76412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
77b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  mirror::ArtMethod* Method() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
784345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    ScopedObjectAccessUnchecked soa(Thread::Current());
794345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    return soa.DecodeMethod(method_);
80545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
81b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
82b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  void SetMethod(mirror::ArtMethod* m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
83b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    ScopedObjectAccessUnchecked soa(Thread::Current());
844345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    method_ = soa.EncodeMethod(m);
85b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
86b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
87b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  uint32_t DexPc() const {
88b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    return dex_pc_;
89b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
90b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
91b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  void SetDexPc(uint32_t pc) {
92b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    dex_pc_ = pc;
93b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
94b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
95b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi private:
964345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  jmethodID method_;
97b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  uint32_t dex_pc_;
98545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
99545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
1004345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartierjobject Dbg::TypeCache::Add(mirror::Class* t) {
1014345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  ScopedObjectAccessUnchecked soa(Thread::Current());
1024345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  int32_t hash_code = t->IdentityHashCode();
1034345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  auto range = objects_.equal_range(hash_code);
1044345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  for (auto it = range.first; it != range.second; ++it) {
1054345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    if (soa.Decode<mirror::Class*>(it->second) == t) {
1064345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier      // Found a matching weak global, return it.
1074345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier      return it->second;
1084345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    }
1094345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  }
1104345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  JNIEnv* env = soa.Env();
1114345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  const jobject local_ref = soa.AddLocalReference<jobject>(t);
1124345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  const jobject weak_global = env->NewWeakGlobalRef(local_ref);
1134345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  env->DeleteLocalRef(local_ref);
1144345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  objects_.insert(std::make_pair(hash_code, weak_global));
1154345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  return weak_global;
1164345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier}
1174345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier
1184345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartiervoid Dbg::TypeCache::Clear() {
1194345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  ScopedObjectAccess soa(Thread::Current());
1204345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  for (const auto& p : objects_) {
1214345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    soa.Vm()->DeleteWeakGlobalRef(soa.Self(), p.second);
1224345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  }
1234345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  objects_.clear();
1244345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier}
1254345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier
126b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchiclass AllocRecord {
127b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi public:
128b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  AllocRecord() : type_(nullptr), byte_count_(0), thin_lock_id_(0) {}
129b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
130b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  mirror::Class* Type() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1314345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    return down_cast<mirror::Class*>(Thread::Current()->DecodeJObject(type_));
132b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
133b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
134b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  void SetType(mirror::Class* t) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1354345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    type_ = Dbg::GetTypeCache().Add(t);
136b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
137545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
138b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  size_t GetDepth() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
139545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    size_t depth = 0;
140b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    while (depth < kMaxAllocRecordStackDepth && stack_[depth].Method() != NULL) {
141545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      ++depth;
142545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
143545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return depth;
144545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
145412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
146b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  size_t ByteCount() const {
147b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    return byte_count_;
148b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
149b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
150b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  void SetByteCount(size_t count) {
151b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    byte_count_ = count;
152412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
153b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
154b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  uint16_t ThinLockId() const {
155b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    return thin_lock_id_;
156b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
157b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
158b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  void SetThinLockId(uint16_t id) {
159b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    thin_lock_id_ = id;
160b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
161b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
162b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  AllocRecordStackTraceElement* StackElement(size_t index) {
163b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    DCHECK_LT(index, kMaxAllocRecordStackDepth);
164b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    return &stack_[index];
165b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
166b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi
167b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi private:
168b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  jobject type_;  // This is a weak global.
169b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  size_t byte_count_;
170b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  uint16_t thin_lock_id_;
171b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  AllocRecordStackTraceElement stack_[kMaxAllocRecordStackDepth];  // Unused entries have NULL method.
172545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
173545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
1740ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchiclass Breakpoint {
1750ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi public:
1760ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  Breakpoint(mirror::ArtMethod* method, uint32_t dex_pc, bool need_full_deoptimization)
1770ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
1780ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    : method_(nullptr), dex_pc_(dex_pc), need_full_deoptimization_(need_full_deoptimization) {
1790ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    ScopedObjectAccessUnchecked soa(Thread::Current());
1800ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    method_ = soa.EncodeMethod(method);
1810ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  }
182a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
1830ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  Breakpoint(const Breakpoint& other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
1840ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    : method_(nullptr), dex_pc_(other.dex_pc_),
1850ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      need_full_deoptimization_(other.need_full_deoptimization_) {
1860ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    ScopedObjectAccessUnchecked soa(Thread::Current());
1870ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    method_ = soa.EncodeMethod(other.Method());
1880ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  }
189a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
1900ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  mirror::ArtMethod* Method() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1910ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    ScopedObjectAccessUnchecked soa(Thread::Current());
1920ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    return soa.DecodeMethod(method_);
1930ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  }
1943b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
1950ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  uint32_t DexPc() const {
1960ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    return dex_pc_;
1970ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  }
1980ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi
1990ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  bool NeedFullDeoptimization() const {
2000ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    return need_full_deoptimization_;
2013b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2020ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi
2030ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi private:
2040ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  // The location of this breakpoint.
2050ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  jmethodID method_;
2060ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  uint32_t dex_pc_;
2070ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi
2080ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  // Indicates whether breakpoint needs full deoptimization or selective deoptimization.
2090ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  bool need_full_deoptimization_;
2108696433d1b3d8ba15288483b777edd888de69135Elliott Hughes};
2118696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2120ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchistatic std::ostream& operator<<(std::ostream& os, Breakpoint& rhs)
213b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2140ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.Method()).c_str(), rhs.DexPc());
2158696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return os;
2168696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
2178696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2183f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzclass DebugInstrumentationListener FINAL : public instrumentation::InstrumentationListener {
21962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers public:
22062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  DebugInstrumentationListener() {}
22162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual ~DebugInstrumentationListener() {}
22262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
2233f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodEntered(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
2243f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                     uint32_t dex_pc)
2253f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
22662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
22762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
22862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
22962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
2308379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::UpdateDebugger(thread, this_object, method, 0, Dbg::kMethodEntry, nullptr);
23162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
23262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
2333f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodExited(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
2343f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc, const JValue& return_value)
2353f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
23662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
23762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
23862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
23962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
2408379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::UpdateDebugger(thread, this_object, method, dex_pc, Dbg::kMethodExit, &return_value);
24162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
24262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
2433f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodUnwind(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
2443f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc)
2453f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
24662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    // We're not recorded to listen to this kind of event, so complain.
24762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method)
24851db44a194bafc3810a41164a8b39614f10e79dfSebastien Hertz               << " " << dex_pc;
24962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
25062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
2513f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void DexPcMoved(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
2523f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                  uint32_t new_dex_pc)
2533f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2548379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc, 0, nullptr);
25562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
25662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
2573f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void FieldRead(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
2583f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                 uint32_t dex_pc, mirror::ArtField* field)
2593f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2603f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostFieldAccessEvent(method, dex_pc, this_object, field);
26162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
2623f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2633f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void FieldWritten(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
2643f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc, mirror::ArtField* field, const JValue& field_value)
2653f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2663f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostFieldModificationEvent(method, dex_pc, this_object, field, &field_value);
2673f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
2683f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2693f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
2703f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                       mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
2713f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                       mirror::Throwable* exception_object)
2723f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2733f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostException(throw_location, catch_method, catch_dex_pc, exception_object);
2743f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
2753f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2763f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz private:
2773f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener);
27862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers} gDebugInstrumentationListener;
27962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
2804ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes// JDWP is allowed unless the Zygote forbids it.
2814ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesstatic bool gJdwpAllowed = true;
2824ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
283c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line?
2843bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gJdwpConfigured = false;
2853bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
286c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Broken-down JDWP options. (Only valid if IsJdwpConfigured() is true.)
287376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughesstatic JDWP::JdwpOptions gJdwpOptions;
2883bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
2893bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes// Runtime JDWP state.
2903bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic JDWP::JdwpState* gJdwpState = NULL;
2913bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerConnected;  // debugger or DDMS is connected.
2923bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerActive;     // debugger is making requests.
2938696433d1b3d8ba15288483b777edd888de69135Elliott Hughesstatic bool gDisposed;           // debugger called VirtualMachine.Dispose, so we should drop the connection.
2943bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
29547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesstatic bool gDdmThreadNotification = false;
29647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
297767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes// DDMS GC-related settings.
298767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
299767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
300767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmHpsgWhat;
301767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
302767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmNhsgWhat;
303767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
304719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstatic ObjectRegistry* gRegistry = nullptr;
305475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
306545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes// Recent allocation tracking.
307719d1a33f6569864f529e5a3fff59e7bca97aad0Ian RogersMutex* Dbg::alloc_tracker_lock_ = nullptr;
308719d1a33f6569864f529e5a3fff59e7bca97aad0Ian RogersAllocRecord* Dbg::recent_allocation_records_ = nullptr;  // TODO: CircularBuffer<AllocRecord>
309719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_max_ = 0;
310719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_head_ = 0;
311719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_count_ = 0;
3124345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu ChartierDbg::TypeCache Dbg::type_cache_;
313545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
314138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz// Deoptimization support.
3154d25df3f76f864b7629ac8c0046d46997f293d8dSebastien HertzMutex* Dbg::deoptimization_lock_ = nullptr;
3164d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzstd::vector<DeoptimizationRequest> Dbg::deoptimization_requests_;
3174d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzsize_t Dbg::full_deoptimization_event_count_ = 0;
3187ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzsize_t Dbg::delayed_full_undeoptimization_count_ = 0;
319138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
32042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz// Instrumentation event reference counters.
32142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::dex_pc_change_event_ref_count_ = 0;
32242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::method_enter_event_ref_count_ = 0;
32342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::method_exit_event_ref_count_ = 0;
32442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::field_read_event_ref_count_ = 0;
32542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::field_write_event_ref_count_ = 0;
32642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::exception_catch_event_ref_count_ = 0;
32742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzuint32_t Dbg::instrumentation_events_ = 0;
32842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz
329138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz// Breakpoints.
33009bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhaostatic std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_);
3318696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
3323b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid DebugInvokeReq::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
3333b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier                                RootType root_type) {
3343b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (receiver != nullptr) {
3353b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(&receiver, arg, tid, root_type);
3363b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3373b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (thread != nullptr) {
3383b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(&thread, arg, tid, root_type);
3393b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3403b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (klass != nullptr) {
3413b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&klass), arg, tid, root_type);
3423b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3433b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (method != nullptr) {
3443b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&method), arg, tid, root_type);
3453b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3463b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
3473b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
348bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzvoid DebugInvokeReq::Clear() {
349bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  invoke_needed = false;
350bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  receiver = nullptr;
351bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  thread = nullptr;
352bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  klass = nullptr;
353bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  method = nullptr;
354bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
355bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
3563b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid SingleStepControl::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
3573b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier                                   RootType root_type) {
3583b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (method != nullptr) {
3593b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&method), arg, tid, root_type);
3603b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
3613b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
3623b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
363bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzbool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const {
364bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  return dex_pcs.find(dex_pc) == dex_pcs.end();
365bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
366bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
367bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzvoid SingleStepControl::Clear() {
368bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  is_active = false;
369bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  method = nullptr;
370bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  dex_pcs.clear();
371bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
372bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
373ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic bool IsBreakpoint(const mirror::ArtMethod* m, uint32_t dex_pc)
37409bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    LOCKS_EXCLUDED(Locks::breakpoint_lock_)
375b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
37609bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
377138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
3780ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    if (gBreakpoints[i].DexPc() == dex_pc && gBreakpoints[i].Method() == m) {
3798696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i];
3808696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      return true;
3818696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
3828696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
3838696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return false;
3848696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
3858696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
38652d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertzstatic bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread)
38752d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) {
3889e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
3899e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // A thread may be suspended for GC; in this code, we really want to know whether
3909e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // there's a debugger suspension active.
3919e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0;
3929e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes}
3939e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
3942dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status)
395b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3962dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
39764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
398436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
399436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
400436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
401436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsArrayInstance()) {
402436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_ARRAY;
403436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
404436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
405436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
406436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsArray();
407436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
408436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
4092dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError& status)
410b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
41264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
413436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
414436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
415436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
416436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsClass()) {
417436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_CLASS;
418436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
419436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
420436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
421436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsClass();
422436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
423436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
424221229cb523f849f165fdafbf9785010963715daElliott Hughesstatic JDWP::JdwpError DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, Thread*& thread)
425a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
426b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
427b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4282dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_peer = gRegistry->Get<mirror::Object*>(thread_id);
42964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_peer == NULL || thread_peer == ObjectRegistry::kInvalidObject) {
430221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't even an object.
431221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_OBJECT;
432221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
433221229cb523f849f165fdafbf9785010963715daElliott Hughes
4342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
435221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) {
436221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't a thread.
437221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_THREAD;
438221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
439221229cb523f849f165fdafbf9785010963715daElliott Hughes
440221229cb523f849f165fdafbf9785010963715daElliott Hughes  thread = Thread::FromManagedThread(soa, thread_peer);
441221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (thread == NULL) {
442221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This is a java.lang.Thread without a Thread*. Must be a zombie.
443221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_THREAD_NOT_ALIVE;
444436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
445221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
446436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
447436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
44824437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) {
44924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // JDWP deliberately uses the descriptor characters' ASCII values for its enum.
45024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // Note that by "basic" we mean that we don't get more specific than JT_OBJECT.
45124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  return static_cast<JDWP::JdwpTag>(descriptor[0]);
45224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
45324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
4549837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogersstatic JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c)
455b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
45686b0010c79ef95b5333cd540b7d3af34a9f1a643Elliott Hughes  CHECK(c != NULL);
45724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsArrayClass()) {
45824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_ARRAY;
45924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
46024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsStringClass()) {
46124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_STRING;
4629837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
4639837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  if (c->IsClassClass()) {
46424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_CLASS_OBJECT;
46524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
4669837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
4679837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* thread_class = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
4689837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (thread_class->IsAssignableFrom(c)) {
4699837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_THREAD;
4709837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
4719837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
4729837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
4739837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* thread_group_class =
4749837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
4759837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (thread_group_class->IsAssignableFrom(c)) {
4769837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_THREAD_GROUP;
4779837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
4789837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
4799837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
4809837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* class_loader_class =
4819837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader);
4829837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (class_loader_class->IsAssignableFrom(c)) {
4839837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_CLASS_LOADER;
4849837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
4859837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
4869837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  return JDWP::JT_OBJECT;
48724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
48824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
48924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes/*
49024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Objects declared to hold Object might actually hold a more specific
49124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * type.  The debugger may take a special interest in these (e.g. it
49224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * wants to display the contents of Strings), so we want to return an
49324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * appropriate tag.
49424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes *
49524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Null objects are tagged JT_OBJECT.
49624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes */
4979837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogersstatic JDWP::JdwpTag TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o)
498b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4999837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass());
50024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
50124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
50224437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic bool IsPrimitiveTag(JDWP::JdwpTag tag) {
50324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  switch (tag) {
50424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BOOLEAN:
50524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BYTE:
50624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_CHAR:
50724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_FLOAT:
50824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_DOUBLE:
50924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_INT:
51024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_LONG:
51124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_SHORT:
51224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_VOID:
51324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return true;
51424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  default:
51524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return false;
51624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
51724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
51824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
5193bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
5203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Handle one of the JDWP name/value pairs.
5213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
5223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * JDWP options are:
5233bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  help: if specified, show help message and bail
5243bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  transport: may be dt_socket or dt_shmem
5253bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  address: for dt_socket, "host:port", or just "port" when listening
5263bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  server: if "y", wait for debugger to attach; if "n", attach to debugger
5273bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  timeout: how long to wait for debugger to connect / listen
5283bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
5293bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Useful with server=n (these aren't supported yet):
5303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onthrow=<exception-name>: connect to debugger when exception thrown
5313bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onuncaught=y|n: connect to debugger when uncaught exception thrown
5323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  launch=<command-line>: launch the debugger itself
5333bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
5343bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * The "transport" option is required, as is "address" if server=n.
5353bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
5363bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool ParseJdwpOption(const std::string& name, const std::string& value) {
5373bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (name == "transport") {
5383bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "dt_socket") {
539376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
5403bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "dt_android_adb") {
541376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
5423bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
5433bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP transport not supported: " << value;
5443bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5453bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5463bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "server") {
5473bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
548376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = false;
5493bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
550376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = true;
5513bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
5523bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
5533bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5543bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5553bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "suspend") {
5563bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
557376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = false;
5583bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
559376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = true;
5603bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
5613bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
5623bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5633bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5643bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "address") {
5653bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* this is either <port> or <host>:<port> */
5663bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string port_string;
567376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.host.clear();
5683bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type colon = value.find(':');
5693bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (colon != std::string::npos) {
570376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.host = value.substr(0, colon);
5713bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value.substr(colon + 1);
5723bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
5733bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value;
5743bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5753bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (port_string.empty()) {
5763bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address missing port: " << value;
5773bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5783bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5793bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    char* end;
580ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    uint64_t port = strtoul(port_string.c_str(), &end, 10);
581ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    if (*end != '\0' || port > 0xffff) {
5823bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address has junk in port field: " << value;
5833bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5843bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
585376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.port = port;
5863bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
5873bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* valid but unsupported */
5883bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
5893bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
5903bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
5913bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
5923bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5933bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
5943bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
5953bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5963bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
5973bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
5983bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * "transport=dt_socket,address=8000,server=y,suspend=n"
5993bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
6003bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesbool Dbg::ParseJdwpOptions(const std::string& options) {
6014dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "ParseJdwpOptions: " << options;
60247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
6033bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  std::vector<std::string> pairs;
6043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Split(options, ',', pairs);
6053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
6063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  for (size_t i = 0; i < pairs.size(); ++i) {
6073bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type equals = pairs[i].find('=');
6083bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (equals == std::string::npos) {
6093bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
6103bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
6113bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
6123bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
6133bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
6143bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
615376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
6163bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP transport: " << options;
6173bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
618376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
6193bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
6203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    return false;
6213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
6223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
6233bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gJdwpConfigured = true;
6243bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
6253bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
6263bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
627d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StartJdwp() {
628c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!gJdwpAllowed || !IsJdwpConfigured()) {
629376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    // No JDWP for you!
630376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    return;
631376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  }
632376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes
633719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  CHECK(gRegistry == nullptr);
634475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gRegistry = new ObjectRegistry;
635475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
636719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  alloc_tracker_lock_ = new Mutex("AllocTracker lock");
6374d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  deoptimization_lock_ = new Mutex("deoptimization lock", kDeoptimizationLock);
638d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // Init JDWP if the debugger is enabled. This may connect out to a
639d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger, passively listen for a debugger, or block waiting for a
640d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger.
641376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
642376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState == NULL) {
643f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // We probably failed because some other process has the port already, which means that
644f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // if we don't abort the user is likely to think they're talking to us when they're actually
645f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // talking to that other process.
6463d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Debugger thread failed to initialize";
647d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
648d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes
649d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // If a debugger has already attached, send the "welcome" message.
650d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // This may cause us to suspend all threads.
651376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState->IsActive()) {
65200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
653376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    if (!gJdwpState->PostVMStart()) {
6543d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes      LOG(WARNING) << "Failed to post 'start' message to debugger";
655d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes    }
656d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
657872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
658872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
659d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StopJdwp() {
6600376e6b62bfb43b515b791f0a7afed2d0d8030bcSebastien Hertz  // Prevent the JDWP thread from processing JDWP incoming packets after we close the connection.
6610376e6b62bfb43b515b791f0a7afed2d0d8030bcSebastien Hertz  Disposed();
662376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  delete gJdwpState;
663719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  gJdwpState = nullptr;
664475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  delete gRegistry;
665719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  gRegistry = nullptr;
666719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  delete alloc_tracker_lock_;
667719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  alloc_tracker_lock_ = nullptr;
6684d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  delete deoptimization_lock_;
6694d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  deoptimization_lock_ = nullptr;
670872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
671872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
672767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesvoid Dbg::GcDidFinish() {
673767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
67400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
6754d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Sending heap info to DDM";
6767162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(gDdmHpifWhen);
677767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
678767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
67900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
6804d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Dumping heap to DDM";
6816a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(false);
682767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
683767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
68400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
6854d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Dumping native heap to DDM";
6866a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(true);
687767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
688767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
689767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
6904ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesvoid Dbg::SetJdwpAllowed(bool allowed) {
6914ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes  gJdwpAllowed = allowed;
6924ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes}
6934ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
694872d4ec7225444d9400d30f9027247deb91012fdElliott HughesDebugInvokeReq* Dbg::GetInvokeReq() {
695475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return Thread::Current()->GetInvokeReq();
696475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
697475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
698475fc23a4a7f35d1be87ea0b06c80df317a720acElliott HughesThread* Dbg::GetDebugThread() {
699475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
700475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
701475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
702475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::ClearWaitForEventThread() {
703475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gJdwpState->ClearWaitForEventThread();
704872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
705872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
706872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Connected() {
7073bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(!gDebuggerConnected);
7084dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "JDWP has attached";
7093bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gDebuggerConnected = true;
7108696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = false;
7118696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
7128696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
7138696433d1b3d8ba15288483b777edd888de69135Elliott Hughesvoid Dbg::Disposed() {
7148696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = true;
7158696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
7168696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
7178696433d1b3d8ba15288483b777edd888de69135Elliott Hughesbool Dbg::IsDisposed() {
7188696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return gDisposed;
719872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
720872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
721a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::GoActive() {
722a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Enable all debugging features, including scans for breakpoints.
723a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // This is a no-op if we're already active.
724a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Only called from the JDWP handler thread.
725a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (gDebuggerActive) {
726a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return;
727a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
728a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
729c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  {
730c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected?
73109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
732c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    CHECK_EQ(gBreakpoints.size(), 0U);
733c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  }
734a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
735138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  {
7364d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(Thread::Current(), *deoptimization_lock_);
7374d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    CHECK_EQ(deoptimization_requests_.size(), 0U);
7384d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    CHECK_EQ(full_deoptimization_event_count_, 0U);
7397ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    CHECK_EQ(delayed_full_undeoptimization_count_, 0U);
74042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(dex_pc_change_event_ref_count_, 0U);
74142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(method_enter_event_ref_count_, 0U);
74242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(method_exit_event_ref_count_, 0U);
74342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(field_read_event_ref_count_, 0U);
74442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(field_write_event_ref_count_, 0U);
74542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(exception_catch_event_ref_count_, 0U);
746138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
747138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
74862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
74962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
75062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
75162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
75262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_NE(old_state, kRunnable);
753138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetInstrumentation()->EnableDeoptimization();
75442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz  instrumentation_events_ = 0;
755a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  gDebuggerActive = true;
75662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
75762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
75862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
75962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  LOG(INFO) << "Debugger is active";
760872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
761872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
762872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Disconnected() {
763234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  CHECK(gDebuggerConnected);
764234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
765c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  LOG(INFO) << "Debugger is no longer active";
766234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
76762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread
76862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener
76962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // and clear the object registry.
77062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
77162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
77262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
77362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
774aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz
775aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  // Debugger may not be active at this point.
776aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  if (gDebuggerActive) {
777aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    {
778aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // Since we're going to disable deoptimization, we clear the deoptimization requests queue.
779aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // This prevents us from having any pending deoptimization request when the debugger attaches
780aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // to us again while no event has been requested yet.
7814d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      MutexLock mu(Thread::Current(), *deoptimization_lock_);
7824d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.clear();
7834d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      full_deoptimization_event_count_ = 0U;
7847ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      delayed_full_undeoptimization_count_ = 0U;
785aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    }
78642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    if (instrumentation_events_ != 0) {
78742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener,
78842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz                                                    instrumentation_events_);
78942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      instrumentation_events_ = 0;
79042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    }
791aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    runtime->GetInstrumentation()->DisableDeoptimization();
792aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    gDebuggerActive = false;
793aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  }
794234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gRegistry->Clear();
795234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gDebuggerConnected = false;
79662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
79762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
798872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
799872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
800c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsDebuggerActive() {
8013bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gDebuggerActive;
802872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
803872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
804c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsJdwpConfigured() {
8053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gJdwpConfigured;
806872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
807872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
808872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesint64_t Dbg::LastDebuggerActivity() {
809ca9515205010099d006ac2fac244348a1e673dcbElliott Hughes  return gJdwpState->LastDebuggerActivity();
810872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
811872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
812872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::UndoDebuggerSuspensions() {
813234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
814872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
815872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
81688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetClassName(JDWP::RefTypeId class_id) {
8172dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id);
8183d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (o == NULL) {
819436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return "NULL";
8207b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
82164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
82288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id));
8233d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
8243d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (!o->IsClass()) {
8257934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    return StringPrintf("non-class %p", o);  // This is only used for debugging output anyway.
8263d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
827f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  return DescriptorToName(o->AsClass()->GetDescriptor().c_str());
8283d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes}
8293d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
83088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) {
831436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
8322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
833436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
834436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
8352435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
83688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  class_object_id = gRegistry->Add(c);
837436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
8388696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
8398696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
84088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) {
8413d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
8422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
8433d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c == NULL) {
8443d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
8453d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
8463d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c->IsInterface()) {
8473d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    // http://code.google.com/p/android/issues/detail?id=20856
84888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = 0;
8493d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else {
85088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = gRegistry->Add(c->GetSuperClass());
8513d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
8523d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
853872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
854872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
855436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
8562dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
85764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
858436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
859436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
860436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader()));
861436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
862872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
863872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
864436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
865436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
8662dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
867436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
868436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
8697b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
870436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
871436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask;
872436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
873de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set,
874de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  // not interfaces.
875436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
876de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  if ((access_flags & kAccInterface) == 0) {
877de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban    access_flags |= kAccSuper;
878de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  }
879436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
880436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAdd4BE(pReply, access_flags);
881436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
882436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
883872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
884872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
885f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott HughesJDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply)
886f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
8872dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
88864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
889f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
890f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
891f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
892f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  // Ensure all threads are suspended while we read objects' lock words.
893f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Thread* self = Thread::Current();
8945426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  CHECK_EQ(self->GetState(), kRunnable);
8955426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  self->TransitionFromRunnableToSuspended(kSuspended);
8965426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  Runtime::Current()->GetThreadList()->SuspendAll();
897f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
898f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  MonitorInfo monitor_info(o);
899f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
9005426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  Runtime::Current()->GetThreadList()->ResumeAll();
9015426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  self->TransitionFromSuspendedToRunnable();
902f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
903d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (monitor_info.owner_ != NULL) {
904d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer()));
905f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else {
906f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    expandBufAddObjectId(reply, gRegistry->Add(NULL));
907f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
908d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.entry_count_);
909d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.waiters_.size());
910d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) {
911d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer()));
912f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
913f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  return JDWP::ERR_NONE;
914f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes}
915f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
916734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott HughesJDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id,
917734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes                                      std::vector<JDWP::ObjectId>& monitors,
91852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                      std::vector<uint32_t>& stack_depths) {
9194993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  struct OwnedMonitorVisitor : public StackVisitor {
920b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    OwnedMonitorVisitor(Thread* thread, Context* context,
921cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi                        std::vector<JDWP::ObjectId>* monitor_vector,
922b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi                        std::vector<uint32_t>* stack_depth_vector)
9234993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
924b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      : StackVisitor(thread, context), current_stack_depth(0),
925b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi        monitors(monitor_vector), stack_depths(stack_depth_vector) {}
9264993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
9274993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
9284993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // annotalysis.
9294993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
9304993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      if (!GetMethod()->IsRuntimeMethod()) {
9314993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        Monitor::VisitLocks(this, AppendOwnedMonitors, this);
932734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes        ++current_stack_depth;
9334993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      }
9344993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      return true;
9354993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
9364993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
937cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi    static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg)
938cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
9397a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg);
940cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi      visitor->monitors->push_back(gRegistry->Add(owned_monitor));
941b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      visitor->stack_depths->push_back(visitor->current_stack_depth);
9424993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
9434993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
944734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    size_t current_stack_depth;
945cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi    std::vector<JDWP::ObjectId>* monitors;
946b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    std::vector<uint32_t>* stack_depths;
9474993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  };
9484993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
949b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  ScopedObjectAccessUnchecked soa(Thread::Current());
950cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi  Thread* thread;
951b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  {
952b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
953b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
954b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    if (error != JDWP::ERR_NONE) {
955b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      return error;
956b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    }
957b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    if (!IsSuspendedForDebugger(soa, thread)) {
958b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      return JDWP::ERR_THREAD_NOT_SUSPENDED;
959b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    }
9604993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
961cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi  std::unique_ptr<Context> context(Context::Create());
962cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi  OwnedMonitorVisitor visitor(thread, context.get(), &monitors, &stack_depths);
963cc8c5c5433230818cfe31617308198f286cc2ee1Hiroshi Yamauchi  visitor.WalkStack();
9644993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  return JDWP::ERR_NONE;
9654993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes}
9664993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
96752d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien HertzJDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id,
96852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                         JDWP::ObjectId& contended_monitor) {
969b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  mirror::Object* contended_monitor_obj;
970f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
971b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  {
972b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
973b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    Thread* thread;
974b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
975b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    if (error != JDWP::ERR_NONE) {
976b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      return error;
977b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    }
978b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    if (!IsSuspendedForDebugger(soa, thread)) {
979b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      return JDWP::ERR_THREAD_NOT_SUSPENDED;
980b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    }
981b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    contended_monitor_obj = Monitor::GetContendedMonitor(thread);
982f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
983b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  // Add() requires the thread_list_lock_ not held to avoid the lock
984b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  // level violation.
985b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  contended_monitor = gRegistry->Add(contended_monitor_obj);
986f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
987f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
988f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
989ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott HughesJDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids,
990ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes                                       std::vector<uint64_t>& counts)
991ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
992412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
993412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
9942dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
995ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  counts.clear();
996ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  for (size_t i = 0; i < class_ids.size(); ++i) {
997ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    JDWP::JdwpError status;
9982dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_ids[i], status);
999ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    if (c == NULL) {
1000ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes      return status;
1001ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    }
1002ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    classes.push_back(c);
1003ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    counts.push_back(0);
1004ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  }
1005412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CountInstances(classes, false, &counts[0]);
1006ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  return JDWP::ERR_NONE;
1007ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes}
1008ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes
10093b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott HughesJDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, std::vector<JDWP::ObjectId>& instances)
10103b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1011412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
1012412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  // We only want reachable instances, so do a GC.
1013412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
10143b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  JDWP::JdwpError status;
10152dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1016412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (c == nullptr) {
10173b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    return status;
10183b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
10192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
10203b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances);
10213b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
10223b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    instances.push_back(gRegistry->Add(raw_instances[i]));
10233b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
10243b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  return JDWP::ERR_NONE;
10253b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes}
10263b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes
10270cbaff584244ee767027aff35cd3c625aaee2994Elliott HughesJDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count,
10280cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes                                         std::vector<JDWP::ObjectId>& referring_objects)
10290cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1030412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
1031412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
10322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
103364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
10340cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
10350cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
10362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
1037412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->GetReferringObjects(o, max_count, raw_instances);
10380cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
10390cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    referring_objects.push_back(gRegistry->Add(raw_instances[i]));
10400cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
10410cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  return JDWP::ERR_NONE;
10420cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes}
10430cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes
104464f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id)
104564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1046e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
1047e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
1048e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
1049e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
105064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisableCollection(object_id);
105164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
105264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
105364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
105464f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id)
105564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1056e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
1057e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI
1058e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // also ignores these cases and never return an error. However it's not obvious why this command
1059e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // should behave differently from DisableCollection and IsCollected commands. So let's be more
1060e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // strict and return an error if this happens.
1061e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
1062e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
1063e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
106464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->EnableCollection(object_id);
106564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
106664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
106764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
106864f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected)
106964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
107065637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  if (object_id == 0) {
107165637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    // Null object id is invalid.
107265637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
107365637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  }
1074e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However
107565637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  // the RI seems to ignore this and assume object has been collected.
107665637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
1077e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
107865637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    is_collected = true;
107965637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  } else {
108065637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    is_collected = gRegistry->IsCollected(object_id);
1081e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
108264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
108364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
108464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
108564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count)
108664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
108764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisposeObject(object_id, reference_count);
108864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
108964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
10904d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertzstatic JDWP::JdwpTypeTag GetTypeTag(mirror::Class* klass)
10914d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
10924d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  DCHECK(klass != nullptr);
10934d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  if (klass->IsArrayClass()) {
10944d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_ARRAY;
10954d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  } else if (klass->IsInterface()) {
10964d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_INTERFACE;
10974d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  } else {
10984d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_CLASS;
10994d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  }
11004d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz}
11014d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz
110288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1103436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11042dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1105436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1106436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
11077b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1108436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
11094d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag type_tag = GetTypeTag(c);
11104d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  expandBufAdd1(pReply, type_tag);
111188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  expandBufAddRefTypeId(pReply, class_id);
1112436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1113872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1114872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
11157b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughesvoid Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) {
1116a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Get the complete list of reference classes (i.e. all classes except
1117a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // the primitive types).
1118a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Returns a newly-allocated buffer full of RefTypeId values.
1119a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  struct ClassListCreator {
1120ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) {
11217b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    }
11227b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
11232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static bool Visit(mirror::Class* c, void* arg) {
1124a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return reinterpret_cast<ClassListCreator*>(arg)->Visit(c);
1125a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1126a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
112764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
112864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
112964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS {
1130a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (!c->IsPrimitive()) {
113164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes        classes.push_back(gRegistry->AddRefType(c));
1132a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
1133a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return true;
1134a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1135a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
11367b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    std::vector<JDWP::RefTypeId>& classes;
1137a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
1138a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
11397b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  ClassListCreator clc(classes);
1140a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc);
1141872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1142872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
114388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) {
1144436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11452dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1146436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1147436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
11487b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
11497b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
1150a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (c->IsArrayClass()) {
1151a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
1152a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = JDWP::TT_ARRAY;
1153a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
1154a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsErroneous()) {
1155a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_ERROR;
1156a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    } else {
1157a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED;
1158a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1159a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
1160a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
1161a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
1162a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (pDescriptor != NULL) {
1163f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier    *pDescriptor = c->GetDescriptor();
1164a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
1165436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1166872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1167872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1168c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughesvoid Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) {
11692dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
11706fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes);
11716fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  ids.clear();
11726fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  for (size_t i = 0; i < classes.size(); ++i) {
11736fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes    ids.push_back(gRegistry->Add(classes[i]));
11746fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  }
1175872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1176872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
117764f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply)
117864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
11792dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
118064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
11812435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
11822435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
11832435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
11844d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass());
118564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass());
11862435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
11872435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAdd1(pReply, type_tag);
11882435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAddRefTypeId(pReply, type_id);
11892435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
11902435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1191872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1192872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1193fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan RogersJDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) {
11941fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  JDWP::JdwpError status;
11952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
11961fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  if (c == NULL) {
11971fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes    return status;
11987b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1199f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  *signature = c->GetDescriptor();
12001fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  return JDWP::ERR_NONE;
1201872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1202872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
120388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result) {
1204436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
12052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
12064206eb5d86d3a2406361e59b2018152b2485ccedSebastien Hertz  if (c == nullptr) {
1207436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
12087b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
12094206eb5d86d3a2406361e59b2018152b2485ccedSebastien Hertz  const char* source_file = c->GetSourceFile();
12104206eb5d86d3a2406361e59b2018152b2485ccedSebastien Hertz  if (source_file == nullptr) {
1211b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz    return JDWP::ERR_ABSENT_INFORMATION;
1212b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz  }
12134206eb5d86d3a2406361e59b2018152b2485ccedSebastien Hertz  result = source_file;
1214436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1215872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1216872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
121788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) {
12189837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
12192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
122064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
1221546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
1222546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
12239837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  tag = TagFromObject(soa, o);
1224546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  return JDWP::ERR_NONE;
1225872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1226872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1227aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughessize_t Dbg::GetTagWidth(JDWP::JdwpTag tag) {
1228dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  switch (tag) {
1229dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_VOID:
1230dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 0;
1231dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BYTE:
1232dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BOOLEAN:
1233dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 1;
1234dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CHAR:
1235dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_SHORT:
1236dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 2;
1237dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_FLOAT:
1238dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_INT:
1239dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 4;
1240dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_ARRAY:
1241dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_OBJECT:
1242dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_STRING:
1243dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD:
1244dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD_GROUP:
1245dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_LOADER:
1246dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_OBJECT:
1247dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return sizeof(JDWP::ObjectId);
1248dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_DOUBLE:
1249dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_LONG:
1250dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 8;
1251dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  default:
12523d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Unknown tag " << tag;
1253dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return -1;
1254dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1255872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1256872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
125788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int& length) {
12583d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
12592dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
12603d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (a == NULL) {
12613d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
126224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
12633d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  length = a->GetLength();
12643d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1265872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1266872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
126788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) {
12683d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
12692dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
12709837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  if (a == nullptr) {
12713d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
12723d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
127324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
127424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) {
127524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
12763d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
127724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
1278f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  std::string descriptor(a->GetClass()->GetDescriptor());
127924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
128024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
12813d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd1(pReply, tag);
12823d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd4BE(pReply, count);
12833d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
128424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (IsPrimitiveTag(tag)) {
128524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    size_t width = GetTagWidth(tag);
128624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    uint8_t* dst = expandBufAddSpace(pReply, count * width);
128724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    if (width == 8) {
1288ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t), 0));
128924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]);
129024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 4) {
1291ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t), 0));
129224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]);
129324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 2) {
1294ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t), 0));
129524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]);
129624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else {
1297ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t), 0));
129824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      memcpy(dst, &src[offset * width], count * width);
129924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
130024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  } else {
13019837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
13022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>();
130324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    for (int i = 0; i < count; ++i) {
13042dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* element = oa->Get(offset + i);
13059837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element)
13069837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers                                                        : tag;
130724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAdd1(pReply, specific_tag);
130824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAddObjectId(pReply, gRegistry->Add(element));
130924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
131024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
131124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
13123d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1313872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1314872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1315ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogerstemplate <typename T>
1316ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstatic void CopyArrayData(mirror::Array* a, JDWP::Request& src, int offset, int count)
1317ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    NO_THREAD_SAFETY_ANALYSIS {
1318ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  // TODO: fix when annotalysis correctly handles non-member functions.
13194b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  DCHECK(a->GetClass()->IsPrimitiveArray());
13204b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1321ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset));
13224b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  for (int i = 0; i < count; ++i) {
13234b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    *dst++ = src.ReadValue(sizeof(T));
13244b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  }
13254b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
13264b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
132788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count,
13284b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                      JDWP::Request& request)
1329b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
13303d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
13314b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  mirror::Array* dst = DecodeArray(array_id, status);
13324b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (dst == NULL) {
13333d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
13343d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
1335f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
13364b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) {
1337f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
13383d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
1339f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1340f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  std::string descriptor = dst->GetClass()->GetDescriptor();
1341f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
1342f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
1343f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  if (IsPrimitiveTag(tag)) {
1344f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    size_t width = GetTagWidth(tag);
1345f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    if (width == 8) {
13464b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint64_t>(dst, request, offset, count);
1347f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 4) {
13484b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint32_t>(dst, request, offset, count);
1349f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 2) {
13504b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint16_t>(dst, request, offset, count);
1351f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else {
13524b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint8_t>(dst, request, offset, count);
1353f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1354f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  } else {
13554b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>();
1356f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    for (int i = 0; i < count; ++i) {
13574b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      JDWP::ObjectId id = request.ReadObjectId();
13582dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
135964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes      if (o == ObjectRegistry::kInvalidObject) {
1360436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
1361436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes      }
1362d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      oa->Set<false>(offset + i, o);
1363f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1364f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1365f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
13663d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1367872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1368872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
13697b3cdfcca472b779cf8745fb8460935e56229f11Elliott HughesJDWP::ObjectId Dbg::CreateString(const std::string& str) {
13702dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  return gRegistry->Add(mirror::String::AllocFromModifiedUtf8(Thread::Current(), str.c_str()));
1371872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1372872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
137388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) {
1374436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
13752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1376436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1377436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
13787b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
137950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  new_object = gRegistry->Add(c->AllocObject(Thread::Current()));
1380436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1381872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1382872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1383bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes/*
1384bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".
1385bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes */
138688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length,
138700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                       JDWP::ObjectId& new_array) {
1388436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
13892dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(array_class_id, status);
1390436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1391436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
13927b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
13936fac447555dc94a935b78198479cce645c837b89Ian Rogers  new_array = gRegistry->Add(mirror::Array::Alloc<true>(Thread::Current(), c, length,
13946fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                        c->GetComponentSize(),
13956fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                        Runtime::Current()->GetHeap()->GetCurrentAllocator()));
1396436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1397872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1398872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
139988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesbool Dbg::MatchType(JDWP::RefTypeId instance_class_id, JDWP::RefTypeId class_id) {
1400436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14012dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c1 = DecodeClass(instance_class_id, status);
1402a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  CHECK(c1 != NULL);
14032dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c2 = DecodeClass(class_id, status);
1404a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  CHECK(c2 != NULL);
1405123756a041baf8421ed933312605daa5ef082f6fSebastien Hertz  return c2->IsAssignableFrom(c1);
1406872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1407872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1408ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::FieldId ToFieldId(const mirror::ArtField* f)
1409b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1410590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
141103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f));
141203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
141303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1414ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::MethodId ToMethodId(const mirror::ArtMethod* m)
1415b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1416590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
141703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m));
141803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
141903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1420ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtField* FromFieldId(JDWP::FieldId fid)
1421b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1422590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
1423ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtField*>(static_cast<uintptr_t>(fid));
1424aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes}
1425aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1426ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtMethod* FromMethodId(JDWP::MethodId mid)
1427b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1428590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
1429ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtMethod*>(static_cast<uintptr_t>(mid));
143003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
143103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1432ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic void SetLocation(JDWP::JdwpLocation& location, mirror::ArtMethod* m, uint32_t dex_pc)
1433b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
143491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (m == NULL) {
143591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    memset(&location, 0, sizeof(location));
143691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  } else {
14372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = m->GetDeclaringClass();
14384d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    location.type_tag = GetTypeTag(c);
1439cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    location.class_id = gRegistry->AddRefType(c);
1440748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    location.method_id = ToMethodId(m);
1441cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    location.dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint64_t>(-1) : dex_pc;
144291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
1443d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes}
1444d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
1445a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetMethodName(JDWP::MethodId method_id)
1446b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1447ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
1448bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  return m->GetName();
1449872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1450872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1451a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetFieldName(JDWP::FieldId field_id)
1452a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
145361c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  return FromFieldId(field_id)->GetName();
1454a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes}
1455a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes
1456a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes/*
1457a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * Augment the access flags for synthetic methods and fields by setting
1458a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * the (as described by the spec) "0xf0000000 bit".  Also, strip out any
1459a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * flags not specified by the Java programming language.
1460a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes */
1461a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughesstatic uint32_t MangleAccessFlags(uint32_t accessFlags) {
1462a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  accessFlags &= kAccJavaFlagsMask;
1463a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  if ((accessFlags & kAccSynthetic) != 0) {
1464a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    accessFlags |= 0xf0000000;
1465a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1466a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return accessFlags;
1467872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1468872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1469dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes/*
1470b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come first. Debuggers
1471b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * expect slots to begin with arguments, but dex code places them at
1472b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * the end.
1473dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes */
1474b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Haostatic uint16_t MangleSlot(uint16_t slot, mirror::ArtMethod* m)
1475b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1476bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
1477cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1478cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // We should not get here for a method without code (native, proxy or abstract). Log it and
1479cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // return the slot as is since all registers are arguments.
1480cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    LOG(WARNING) << "Trying to mangle slot for method without code " << PrettyMethod(m);
1481cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return slot;
1482cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1483b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1484b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1485b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot >= locals_size) {
1486b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - locals_size;
1487b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1488b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + ins_size;
1489dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1490dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1491dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1492b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao/*
1493b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come last. Reverts
1494b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * slots to dex style argument placement.
1495b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao */
1496ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic uint16_t DemangleSlot(uint16_t slot, mirror::ArtMethod* m)
1497b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1498bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
1499cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1500cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // We should not get here for a method without code (native, proxy or abstract). Log it and
1501cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // return the slot as is since all registers are arguments.
1502cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    LOG(WARNING) << "Trying to demangle slot for method without code " << PrettyMethod(m);
1503cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return slot;
1504cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1505b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1506b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1507b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot < ins_size) {
1508b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + locals_size;
1509b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1510b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - ins_size;
1511dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1512dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1513dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
151488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1515436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
15162dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1517436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1518436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
15197b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1520a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1521a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t instance_field_count = c->NumInstanceFields();
1522a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t static_field_count = c->NumStaticFields();
1523a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1524a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, instance_field_count + static_field_count);
1525a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1526a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < instance_field_count + static_field_count; ++i) {
1527ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count);
1528a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddFieldId(pReply, ToFieldId(f));
152961c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    expandBufAddUtf8String(pReply, f->GetName());
153061c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier    expandBufAddUtf8String(pReply, f->GetTypeDescriptor());
1531c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1532a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1533a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1534a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1535a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags()));
1536a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1537436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1538a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1539a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
154088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic,
154100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                           JDWP::ExpandBuf* pReply) {
1542436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
15432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1544436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1545436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
15467b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1547a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1548a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t direct_method_count = c->NumDirectMethods();
1549a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t virtual_method_count = c->NumVirtualMethods();
1550a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1551a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, direct_method_count + virtual_method_count);
1552a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1553a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) {
1554ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
1555a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddMethodId(pReply, ToMethodId(m));
1556bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    expandBufAddUtf8String(pReply, m->GetName());
1557bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    expandBufAddUtf8String(pReply, m->GetSignature().ToString());
1558c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1559a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1560a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1561a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1562a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags()));
1563a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1564436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1565a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1566a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
156788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1568436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
1569f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  Thread* self = Thread::Current();
1570f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  StackHandleScope<1> hs(self);
1571f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  Handle<mirror::Class> c(hs.NewHandle(DecodeClass(class_id, status)));
1572f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  if (c.Get() == nullptr) {
1573436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
15747b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1575f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  size_t interface_count = c->NumDirectInterfaces();
1576a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, interface_count);
1577a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < interface_count; ++i) {
1578f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier    expandBufAddRefTypeId(pReply,
1579f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier                          gRegistry->AddRefType(mirror::Class::GetDirectInterface(self, c, i)));
1580a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1581436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1582872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1583872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
158488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply)
1585b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
158603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  struct DebugCallbackContext {
158703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    int numItems;
158803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    JDWP::ExpandBuf* pReply;
158903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
15902435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    static bool Callback(void* context, uint32_t address, uint32_t line_number) {
159103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
159203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      expandBufAdd8BE(pContext->pReply, address);
15932435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      expandBufAdd4BE(pContext->pReply, line_number);
159403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      pContext->numItems++;
1595f2910eef247b45ce1d489e323b36b5de6b6157aaSebastien Hertz      return false;
159603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
159703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
1598ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
1599bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
160003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  uint64_t start, end;
1601cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1602cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    DCHECK(m->IsNative() || m->IsProxyMethod());
160303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = -1;
160403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    end = -1;
160503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  } else {
160603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = 0;
160714f0db92225d34622fa5cb1a6dc9287334aaf6c7jeffhao    // Return the index of the last instruction
1608cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    end = code_item->insns_size_in_code_units_ - 1;
160903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  }
161003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
161103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, start);
161203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, end);
161303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
161403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  // Add numLines later
161503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  size_t numLinesOffset = expandBufGetLength(pReply);
161603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd4BE(pReply, 0);
161703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
161803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  DebugCallbackContext context;
161903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.numItems = 0;
162003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.pReply = pReply;
162103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1622cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item != nullptr) {
1623bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
1624bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                     DebugCallbackContext::Callback, NULL, &context);
1625cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
162603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
162703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems);
1628872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1629872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1630bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartiervoid Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic,
1631bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                              JDWP::ExpandBuf* pReply) {
1632dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  struct DebugCallbackContext {
1633b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    mirror::ArtMethod* method;
1634dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    JDWP::ExpandBuf* pReply;
1635c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    size_t variable_count;
1636c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    bool with_generic;
1637dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1638bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress,
1639bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                         const char* name, const char* descriptor, const char* signature)
1640b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1641dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1642dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1643bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      VLOG(jdwp) << StringPrintf("    %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d",
1644bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                 pContext->variable_count, startAddress, endAddress - startAddress,
1645bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                 name, descriptor, signature, slot,
1646bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                 MangleSlot(slot, pContext->method));
1647dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1648b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao      slot = MangleSlot(slot, pContext->method);
164968fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
1650dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd8BE(pContext->pReply, startAddress);
1651dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, name);
1652dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, descriptor);
1653c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      if (pContext->with_generic) {
1654dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes        expandBufAddUtf8String(pContext->pReply, signature);
1655dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      }
1656dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, endAddress - startAddress);
1657dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, slot);
1658dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1659c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      ++pContext->variable_count;
1660dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    }
1661dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  };
1662ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
1663dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1664c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // arg_count considers doubles and longs to take 2 units.
1665c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // variable_count considers everything to take 1 unit.
1666bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  std::string shorty(m->GetShorty());
1667ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  expandBufAdd4BE(pReply, mirror::ArtMethod::NumArgRegisters(shorty));
1668dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1669c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // We don't know the total number of variables yet, so leave a blank and update it later.
1670c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  size_t variable_count_offset = expandBufGetLength(pReply);
1671dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  expandBufAdd4BE(pReply, 0);
1672dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1673dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  DebugCallbackContext context;
1674b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  context.method = m;
1675dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  context.pReply = pReply;
1676c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.variable_count = 0;
1677c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.with_generic = with_generic;
1678dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1679bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
1680cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item != nullptr) {
1681bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    m->GetDexFile()->DecodeDebugInfo(
1682bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL, DebugCallbackContext::Callback,
1683bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        &context);
1684cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1685dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1686c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count);
1687872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1688872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1689579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value,
1690579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                                  JDWP::ExpandBuf* pReply) {
1691579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  mirror::ArtMethod* m = FromMethodId(method_id);
1692bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(m->GetShorty());
1693579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  OutputJValue(tag, return_value, pReply);
1694579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1695579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
16963f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value,
16973f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                           JDWP::ExpandBuf* pReply) {
16983f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  mirror::ArtField* f = FromFieldId(field_id);
169961c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
17003f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  OutputJValue(tag, field_value, pReply);
17013f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
17023f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
17039777ba230c83a0edcbda2cf7b208339e77bf171bElliott HughesJDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id,
17049777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes                                  std::vector<uint8_t>& bytecodes)
17059777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1706ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
17079777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  if (m == NULL) {
17089777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    return JDWP::ERR_INVALID_METHODID;
17099777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
1710bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
17119777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  size_t byte_count = code_item->insns_size_in_code_units_ * 2;
17129777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_);
17139777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* end = begin + byte_count;
17149777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  for (const uint8_t* p = begin; p != end; ++p) {
17159777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    bytecodes.push_back(*p);
17169777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
17179777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  return JDWP::ERR_NONE;
17189777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes}
17199777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes
172088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) {
172161c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor());
1722872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1723872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
172488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) {
172561c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  return BasicTagFromDescriptor(FromFieldId(field_id)->GetTypeDescriptor());
1726872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1727872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
172888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id,
172988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                         JDWP::FieldId field_id, JDWP::ExpandBuf* pReply,
173000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         bool is_static)
1731b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
17320cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  JDWP::JdwpError status;
17332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(ref_type_id, status);
173488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  if (ref_type_id != 0 && c == NULL) {
17350cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    return status;
17360cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
17370cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
17382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
173964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
17403f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
17413f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1742ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
17430cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
17442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* receiver_class = c;
17450cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class == NULL && o != NULL) {
17460cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    receiver_class = o->GetClass();
17470cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
17480cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we give up now if receiver_class is NULL?
17490cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) {
17500cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class);
17513f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_FIELDID;
17523f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1753aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
17540cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
17550cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
17560cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
17570cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
17580cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
17590cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
17600cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
17610cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
17620cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
17630cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
17640cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
17650dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
17660dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
17670dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
17680cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
176961c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
1770579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  JValue field_value;
1771579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (tag == JDWP::JT_VOID) {
1772579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    LOG(FATAL) << "Unknown tag: " << tag;
1773579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (!IsPrimitiveTag(tag)) {
1774579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetL(f->GetObject(o));
1775579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1776579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetJ(f->Get64(o));
1777aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
1778579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetI(f->Get32(o));
1779aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
1780579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  Dbg::OutputJValue(tag, &field_value, pReply);
1781579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
17823f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  return JDWP::ERR_NONE;
1783872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1784872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
178588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id,
178600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ExpandBuf* pReply) {
178788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(0, object_id, field_id, pReply, false);
17883f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
17893f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
179088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, JDWP::ExpandBuf* pReply) {
179188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true);
17923f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
17933f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
179488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id,
179500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         uint64_t value, int width, bool is_static)
1796b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
17972dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
179864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
17993f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
18003f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1801ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
18020cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
18030cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
18040cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
18050cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
18060cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
18070cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
18080cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
18090cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
18100cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
18110cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
18120cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
18133f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
18140dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
18150dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
18160dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
1817aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
181861c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(f->GetTypeDescriptor());
1819aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1820aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  if (IsPrimitiveTag(tag)) {
1821aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
18221bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_EQ(width, 8);
1823d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      // Debugging can't use transactional mode (runtime only).
1824d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      f->Set64<false>(o, value);
1825aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    } else {
18261bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_LE(width, 4);
1827d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      // Debugging can't use transactional mode (runtime only).
1828d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      f->Set32<false>(o, value);
1829aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    }
1830aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
18312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* v = gRegistry->Get<mirror::Object*>(value);
183264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (v == ObjectRegistry::kInvalidObject) {
18333d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes      return JDWP::ERR_INVALID_OBJECT;
18343d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    }
18353f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    if (v != NULL) {
183661c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier      mirror::Class* field_type;
183761c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier      {
183861c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        StackHandleScope<3> hs(Thread::Current());
183961c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        HandleWrapper<mirror::Object> h_v(hs.NewHandleWrapper(&v));
184061c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        HandleWrapper<mirror::ArtField> h_f(hs.NewHandleWrapper(&f));
184161c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        HandleWrapper<mirror::Object> h_o(hs.NewHandleWrapper(&o));
184261c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier        field_type = FieldHelper(h_f).GetType();
184361c5ebc6aee2cac1c363de6fbdac25ada1697fdbMathieu Chartier      }
18443f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      if (!field_type->IsAssignableFrom(v->GetClass())) {
18453f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
18463f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      }
18473f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
1848d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    // Debugging can't use transactional mode (runtime only).
1849d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    f->SetObject<false>(o, v);
1850aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
18513d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
18523d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1853872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1854872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
185588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value,
185600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   int width) {
185788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(object_id, field_id, value, width, false);
1858872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1859872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
186088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) {
186188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(0, field_id, value, width, true);
1862872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1863872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
186488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::StringToUtf8(JDWP::ObjectId string_id) {
18652dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = gRegistry->Get<mirror::String*>(string_id);
186668fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  return s->ToModifiedUtf8();
1867872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1868872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1869579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) {
1870579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (IsPrimitiveTag(tag)) {
1871579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAdd1(pReply, tag);
1872579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) {
1873579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd1(pReply, return_value->GetI());
1874579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) {
1875579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd2BE(pReply, return_value->GetI());
1876579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) {
1877579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd4BE(pReply, return_value->GetI());
1878579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1879579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd8BE(pReply, return_value->GetJ());
1880579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else {
1881579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      CHECK_EQ(tag, JDWP::JT_VOID);
1882579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    }
1883579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else {
18849837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
1885579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    mirror::Object* value = return_value->GetL();
18869837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    expandBufAdd1(pReply, TagFromObject(soa, value));
1887579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAddObjectId(pReply, gRegistry->Add(value));
1888579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  }
1889579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1890579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
1891221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string& name) {
1892a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  ScopedObjectAccessUnchecked soa(Thread::Current());
1893a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1894221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1895221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1896221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) {
1897221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1898a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1899221229cb523f849f165fdafbf9785010963715daElliott Hughes
1900221229cb523f849f165fdafbf9785010963715daElliott Hughes  // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName.
19012dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
1902ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* java_lang_Thread_name_field =
19032dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      soa.DecodeField(WellKnownClasses::java_lang_Thread_name);
19042dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s =
19052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object));
1906221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (s != NULL) {
1907221229cb523f849f165fdafbf9785010963715daElliott Hughes    name = s->ToModifiedUtf8();
1908221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1909221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1910872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1911872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1912221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
191300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
19142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
191564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_object == ObjectRegistry::kInvalidObject) {
19162435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
19172435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
19189837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroup");
19192435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Okay, so it's an object, but is it actually a thread?
1920b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  JDWP::JdwpError error;
1921b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  {
1922b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1923b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    Thread* thread;
1924b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    error = DecodeThread(soa, thread_id, thread);
1925b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  }
1926221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1927221229cb523f849f165fdafbf9785010963715daElliott Hughes    // Zombie threads are in the null group.
1928221229cb523f849f165fdafbf9785010963715daElliott Hughes    expandBufAddObjectId(pReply, JDWP::ObjectId(0));
192952d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    error = JDWP::ERR_NONE;
193052d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz  } else if (error == JDWP::ERR_NONE) {
193152d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
193252d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    CHECK(c != nullptr);
193352d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::ArtField* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;");
1934b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    CHECK(f != nullptr);
193552d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::Object* group = f->GetObject(thread_object);
1936b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    CHECK(group != nullptr);
193752d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    JDWP::ObjectId thread_group_id = gRegistry->Add(group);
193852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    expandBufAddObjectId(pReply, thread_group_id);
19392435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
19409837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
194152d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz  return error;
1942872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1943872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
194488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id) {
194500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
19462dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
19479837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(thread_group != nullptr);
19489837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroupName");
19499837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
19509837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(c != nullptr);
1951ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("name", "Ljava/lang/String;");
1952499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(f != NULL);
19532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group));
19549837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
1955499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  return s->ToModifiedUtf8();
1956872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1957872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
195888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id) {
19599837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
19602dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
19619837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(thread_group != nullptr);
19629837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroupParent");
19639837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
19649837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(c != nullptr);
1965ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;");
19664e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  CHECK(f != NULL);
19672dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* parent = f->GetObject(thread_group);
19689837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
19694e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  return gRegistry->Add(parent);
1970872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1971872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1972872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetSystemThreadGroupId() {
197300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
1974ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup);
19752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
1976365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
1977872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1978872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1979872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetMainThreadGroupId() {
198000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1981ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup);
19822dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
1983365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
1984872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1985872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1986920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff HaoJDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) {
1987920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  switch (state) {
1988920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kBlocked:
1989920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_MONITOR;
1990920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kNative:
1991920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kRunnable:
1992920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSuspended:
1993920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_RUNNING;
1994920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSleeping:
1995920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_SLEEPING;
1996920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kStarting:
1997920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTerminated:
1998920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_ZOMBIE;
1999920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTimedWaiting:
2000bae182cbc6adc8796154162a87fc54ae804e0469Sebastien Hertz    case kWaitingForCheckPointsToRun:
2001920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSend:
2002920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSuspension:
2003920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerToAttach:
2004138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    case kWaitingForDeoptimization:
2005920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForGcToComplete:
2006920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForJniOnLoad:
2007bae182cbc6adc8796154162a87fc54ae804e0469Sebastien Hertz    case kWaitingForMethodTracingStart:
2008920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForSignalCatcherOutput:
2009920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainDebuggerLoop:
2010920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainSignalCatcherLoop:
2011920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingPerformingGc:
2012920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaiting:
2013920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_WAIT;
2014920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      // Don't add a 'default' here so the compiler can spot incompatible enum changes.
2015920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  }
2016920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  LOG(FATAL) << "Unknown thread state: " << state;
2017920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  return JDWP::TS_ZOMBIE;
2018920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao}
2019920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao
202052d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien HertzJDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus,
202152d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                     JDWP::JdwpSuspendStatus* pSuspendStatus) {
202200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
2023499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
20249e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED;
20259e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
202650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2027221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2028221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2029221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2030221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
2031221229cb523f849f165fdafbf9785010963715daElliott Hughes      *pThreadStatus = JDWP::TS_ZOMBIE;
2032221229cb523f849f165fdafbf9785010963715daElliott Hughes      return JDWP::ERR_NONE;
2033221229cb523f849f165fdafbf9785010963715daElliott Hughes    }
2034221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2035499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  }
2036499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
20379e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  if (IsSuspendedForDebugger(soa, thread)) {
20389e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED;
20399e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  }
204000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
2041920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  *pThreadStatus = ToJdwpThreadStatus(thread->GetState());
2042221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
2043872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2044872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2045221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
204600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
204750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2048221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2049221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2050221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2051221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
20522435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
205350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
205400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  expandBufAdd4BE(pReply, thread->GetDebugSuspendCount());
20552435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
2056872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2057872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2058f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott HughesJDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) {
2059f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccess soa(Thread::Current());
2060f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2061f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
2062f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2063f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
2064f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
2065f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
2066dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  thread->Interrupt(soa.Self());
2067f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
2068f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
2069f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
2070caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughesvoid Dbg::GetThreads(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& thread_ids) {
2071365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  class ThreadListVisitor {
2072365c10235438607541fa2259a5fec48061b90bd8Ian Rogers   public:
20732dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    ThreadListVisitor(const ScopedObjectAccessUnchecked& soa, mirror::Object* desired_thread_group,
207400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                      std::vector<JDWP::ObjectId>& thread_ids)
2075b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
20760dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        : soa_(soa), desired_thread_group_(desired_thread_group), thread_ids_(thread_ids) {}
2077365c10235438607541fa2259a5fec48061b90bd8Ian Rogers
2078a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    static void Visit(Thread* t, void* arg) {
2079a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t);
2080a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
2081a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
208200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
208300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
208400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void Visit(Thread* t) NO_THREAD_SAFETY_ANALYSIS {
2085a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (t == Dbg::GetDebugThread()) {
2086a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and
2087a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        // query all threads, so it's easier if we just don't tell them about this thread.
2088a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
2089a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
20902dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* peer = t->GetPeer();
20910dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      if (IsInDesiredThreadGroup(peer)) {
2092120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers        thread_ids_.push_back(gRegistry->Add(peer));
2093a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
2094a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
2095a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
2096365c10235438607541fa2259a5fec48061b90bd8Ian Rogers   private:
20972dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    bool IsInDesiredThreadGroup(mirror::Object* peer)
20980dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20990dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      // peer might be NULL if the thread is still starting up.
21000dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      if (peer == NULL) {
21010dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // We can't tell the debugger about this thread yet.
21020dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // TODO: if we identified threads to the debugger by their Thread*
21032dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        // rather than their peer's mirror::Object*, we could fix this.
21040dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // Doing so might help us report ZOMBIE threads too.
21050dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        return false;
21060dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      }
2107c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      // Do we want threads from all thread groups?
2108c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      if (desired_thread_group_ == NULL) {
2109c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao        return true;
2110c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      }
21112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* group = soa_.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(peer);
21120dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      return (group == desired_thread_group_);
21130dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    }
21140dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao
2115dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier    const ScopedObjectAccessUnchecked& soa_;
21162dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* const desired_thread_group_;
2117caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    std::vector<JDWP::ObjectId>& thread_ids_;
2118a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
2119a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
212000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
21212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
212200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ThreadListVisitor tlv(soa, thread_group, thread_ids);
212350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2124f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughes  Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv);
2125a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes}
2126a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
2127caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughesvoid Dbg::GetChildThreadGroups(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& child_thread_group_ids) {
212800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
21292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
2130caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes
2131caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Get the ArrayList<ThreadGroup> "groups" out of this thread group...
2132ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* groups_field = thread_group->GetClass()->FindInstanceField("groups", "Ljava/util/List;");
21332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* groups_array_list = groups_field->GetObject(thread_group);
2134872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2135caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Get the array and size out of the ArrayList<ThreadGroup>...
2136ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* array_field = groups_array_list->GetClass()->FindInstanceField("array", "[Ljava/lang/Object;");
2137ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* size_field = groups_array_list->GetClass()->FindInstanceField("size", "I");
21382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::ObjectArray<mirror::Object>* groups_array =
21392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>();
2140caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  const int32_t size = size_field->GetInt(groups_array_list);
2141caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes
2142caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Copy the first 'size' elements out of the array into the result.
2143caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  for (int32_t i = 0; i < size; ++i) {
2144caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    child_thread_group_ids.push_back(gRegistry->Add(groups_array->Get(i)));
2145caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  }
2146872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2147872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
214800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic int GetStackDepth(Thread* thread)
2149b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
21500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct CountStackDepthVisitor : public StackVisitor {
215193ba893c20532990a430741e0a97212900094e8cBrian Carlstrom    explicit CountStackDepthVisitor(Thread* thread)
21527a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth(0) {}
21530399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
215464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
215564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
215664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
21570399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!GetMethod()->IsRuntimeMethod()) {
2158f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes        ++depth;
2159f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes      }
2160530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
2161a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
2162a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    size_t depth;
2163a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  };
216408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
21657a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  CountStackDepthVisitor visitor(thread);
21660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2167a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return visitor.depth;
2168872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2169872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2170221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result) {
217100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
2172a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2173221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2174221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2175221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2176221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2177221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2178f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
2179f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
2180f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
2181221229cb523f849f165fdafbf9785010963715daElliott Hughes  result = GetStackDepth(thread);
2182221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
21838696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
21848696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2185306057fd278d75bf3794bd5243a3b6652c487d18Ian RogersJDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame,
2186306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers                                     size_t frame_count, JDWP::ExpandBuf* buf) {
21876e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  class GetFrameVisitor : public StackVisitor {
21886e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   public:
21897a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    GetFrameVisitor(Thread* thread, size_t start_frame, size_t frame_count, JDWP::ExpandBuf* buf)
2190b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
21917a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth_(0),
21926e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes          start_frame_(start_frame), frame_count_(frame_count), buf_(buf) {
21936e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      expandBufAdd4BE(buf_, frame_count_);
219403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
21950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
219600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
219700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
219800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
21990399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetMethod()->IsRuntimeMethod()) {
22007934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        return true;  // The debugger can't do anything useful with a frame that has no Method*.
220103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
22026e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_ + frame_count_) {
2203530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes        return false;
220403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
22056e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_) {
22066e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::FrameId frame_id(GetFrameId());
22076e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::JdwpLocation location;
22086e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        SetLocation(location, GetMethod(), GetDexPc());
2209ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        VLOG(jdwp) << StringPrintf("    Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location;
22106e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAdd8BE(buf_, frame_id);
22116e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAddLocation(buf_, location);
22126e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      }
22136e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      ++depth_;
2214530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
221503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
22166e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes
22176e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   private:
22186e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    size_t depth_;
22196e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t start_frame_;
22206e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t frame_count_;
22216e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    JDWP::ExpandBuf* buf_;
222203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
222300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
222400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2225a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2226221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2227221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2228221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2229221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2230221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2231f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
2232f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
2233f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
22347a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetFrameVisitor visitor(thread, start_frame, frame_count, buf);
22350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
22366e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
2237872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2238872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2239872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetThreadSelfId() {
2240dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier  ScopedObjectAccessUnchecked soa(Thread::Current());
2241cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  return gRegistry->Add(soa.Self()->GetPeer());
2242872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2243872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2244475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::SuspendVM() {
224500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
2246872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2247872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2248872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ResumeVM() {
2249c61a267e98ed1038b74c33c7740414ced4a27d89Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
2250872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2251872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2252221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) {
225300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedLocalRef<jobject> peer(Thread::Current()->GetJniEnv(), NULL);
225400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
225500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
22562dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id)));
225700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
225800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (peer.get() == NULL) {
225900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
226000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
226100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Suspend thread to build stack trace.
2262f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  bool timed_out;
2263d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  Thread* thread = ThreadList::SuspendThreadByPeer(peer.get(), request_suspension, true,
2264d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                                   &timed_out);
226500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (thread != NULL) {
226600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_NONE;
2267f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else if (timed_out) {
226800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_INTERNAL;
226900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  } else {
227000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
22714e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
2272872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2273872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2274221229cb523f849f165fdafbf9785010963715daElliott Hughesvoid Dbg::ResumeThread(JDWP::ObjectId thread_id) {
227500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
22762dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id);
2277a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  Thread* thread;
2278a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  {
2279a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2280a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    thread = Thread::FromManagedThread(soa, peer);
2281a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  }
22824e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  if (thread == NULL) {
22834e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    LOG(WARNING) << "No such thread for resume: " << peer;
22844e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    return;
22854e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
228600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool needs_resume;
228700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
228850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
228900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    needs_resume = thread->GetSuspendCount() > 0;
229000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
229100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (needs_resume) {
2292546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    Runtime::Current()->GetThreadList()->Resume(thread, true);
2293546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
2294872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2295872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2296872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::SuspendSelf() {
2297475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
2298872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2299872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
23000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct GetThisVisitor : public StackVisitor {
23017a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
2302b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
23037a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, context), this_object(NULL), frame_id(frame_id) {}
230468fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
230500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
230600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
230700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
23086e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    if (frame_id != GetFrameId()) {
23090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return true;  // continue
23100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    } else {
231162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      this_object = GetThisObject();
231262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return false;
23130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
231468fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  }
2315dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
23162dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* this_object;
23176e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  JDWP::FrameId frame_id;
23180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers};
2319ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
232000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersJDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id,
232100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ObjectId* result) {
232200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
232300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* thread;
232400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
232550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2326221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2327221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
2328221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
232900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
23309e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    if (!IsSuspendedForDebugger(soa, thread)) {
233100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      return JDWP::ERR_THREAD_NOT_SUSPENDED;
233200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
23336e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  }
2334700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
23357a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor visitor(thread, context.get(), frame_id);
23360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
23376e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  *result = gRegistry->Add(visitor.this_object);
23386e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
23390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
23400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
2341cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien HertzJDWP::JdwpError Dbg::GetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot,
2342cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                   JDWP::JdwpTag tag, uint8_t* buf, size_t width) {
23430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct GetLocalVisitor : public StackVisitor {
23449837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    GetLocalVisitor(const ScopedObjectAccessUnchecked& soa, Thread* thread, Context* context,
23459837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers                    JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width)
2346b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
23479837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        : StackVisitor(thread, context), soa_(soa), frame_id_(frame_id), slot_(slot), tag_(tag),
2348cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz          buf_(buf), width_(width), error_(JDWP::ERR_NONE) {}
2349ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
235000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
235100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
235200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
23530399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetFrameId() != frame_id_) {
23540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        return true;  // Not our frame, carry on.
23550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
23560399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // TODO: check that the tag is compatible with the actual type of the slot!
2357cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      // TODO: check slot is valid for this method or return INVALID_SLOT error.
2358ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* m = GetMethod();
2359cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      if (m->IsNative()) {
2360cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        // We can't read local value from native method.
2361cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        error_ = JDWP::ERR_OPAQUE_FRAME;
2362cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        return false;
2363cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      }
23640399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      uint16_t reg = DemangleSlot(slot_, m);
23650bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz      constexpr JDWP::JdwpError kFailureErrorCode = JDWP::ERR_ABSENT_INFORMATION;
23660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      switch (tag_) {
23670bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_BOOLEAN: {
23680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
23690bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          uint32_t intVal;
23700bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          if (GetVReg(m, reg, kIntVReg, &intVal)) {
23710bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "get boolean local " << reg << " = " << intVal;
23720bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            JDWP::Set1(buf_+1, intVal != 0);
23730bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          } else {
23740bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to get boolean local " << reg;
23750bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
23760bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          }
23770bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          break;
23780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23790bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_BYTE: {
23800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
23810bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          uint32_t intVal;
23820bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          if (GetVReg(m, reg, kIntVReg, &intVal)) {
23830bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "get byte local " << reg << " = " << intVal;
23840bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            JDWP::Set1(buf_+1, intVal);
23850bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          } else {
23860bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to get byte local " << reg;
23870bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
23880bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          }
23890bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          break;
23900399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23910bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_SHORT:
23920bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_CHAR: {
23930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 2U);
23940bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          uint32_t intVal;
23950bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          if (GetVReg(m, reg, kIntVReg, &intVal)) {
23960bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "get short/char local " << reg << " = " << intVal;
23970bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            JDWP::Set2BE(buf_+1, intVal);
23980bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          } else {
23990bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to get short/char local " << reg;
24000bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
24010bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          }
24020bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          break;
24030399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
24040bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_INT: {
24052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 4U);
24060bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          uint32_t intVal;
24070bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          if (GetVReg(m, reg, kIntVReg, &intVal)) {
24080bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "get int local " << reg << " = " << intVal;
24090bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            JDWP::Set4BE(buf_+1, intVal);
24100bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          } else {
24110bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to get int local " << reg;
24120bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
24130bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          }
24140bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          break;
24152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
24160bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_FLOAT: {
24170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 4U);
24180bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          uint32_t intVal;
24190bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          if (GetVReg(m, reg, kFloatVReg, &intVal)) {
24200bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "get float local " << reg << " = " << intVal;
24210bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            JDWP::Set4BE(buf_+1, intVal);
24220bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          } else {
24230bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to get float local " << reg;
24240bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
24250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
24260bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          break;
24270399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
24280bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_ARRAY:
24290bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_CLASS_LOADER:
24300bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_CLASS_OBJECT:
24310bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_OBJECT:
24320bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_STRING:
24330bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_THREAD:
24340bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_THREAD_GROUP: {
24350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
24360bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          uint32_t intVal;
24370bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          if (GetVReg(m, reg, kReferenceVReg, &intVal)) {
24380bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            mirror::Object* o = reinterpret_cast<mirror::Object*>(intVal);
24390bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "get " << tag_ << " object local " << reg << " = " << o;
24400bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
24410bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz              LOG(FATAL) << "Register " << reg << " expected to hold " << tag_ << " object: " << o;
24420bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            }
24430bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            tag_ = TagFromObject(soa_, o);
24440bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
24450bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          } else {
24460bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to get " << tag_ << " object local " << reg;
24470bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
24480399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
24490bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          break;
24500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
24510bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_DOUBLE: {
24522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 8U);
2453c901dd7bdc80b953d04100ef2f54b8d1ca5f466bSebastien Hertz          uint64_t longVal;
2454c901dd7bdc80b953d04100ef2f54b8d1ca5f466bSebastien Hertz          if (GetVRegPair(m, reg, kDoubleLoVReg, kDoubleHiVReg, &longVal)) {
2455c901dd7bdc80b953d04100ef2f54b8d1ca5f466bSebastien Hertz            VLOG(jdwp) << "get double local " << reg << " = " << longVal;
24560bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            JDWP::Set8BE(buf_+1, longVal);
24570bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          } else {
24580bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to get double local " << reg;
24590bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
24600bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          }
24610bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          break;
24622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
24630bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_LONG: {
24640399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 8U);
2465c901dd7bdc80b953d04100ef2f54b8d1ca5f466bSebastien Hertz          uint64_t longVal;
2466c901dd7bdc80b953d04100ef2f54b8d1ca5f466bSebastien Hertz          if (GetVRegPair(m, reg, kLongLoVReg, kLongHiVReg, &longVal)) {
2467c901dd7bdc80b953d04100ef2f54b8d1ca5f466bSebastien Hertz            VLOG(jdwp) << "get long local " << reg << " = " << longVal;
24680bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            JDWP::Set8BE(buf_+1, longVal);
24690bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          } else {
24700bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to get long local " << reg;
24710bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
24720bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          }
24730bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          break;
24740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
24750bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        default:
24760bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          LOG(FATAL) << "Unknown tag " << tag_;
24770bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          break;
24780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
2479ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
24800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // Prepend tag, which may have been updated.
24810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      JDWP::Set1(buf_, tag_);
24820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return false;
24830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
24849837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    const ScopedObjectAccessUnchecked& soa_;
24850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::FrameId frame_id_;
24860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const int slot_;
24870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    JDWP::JdwpTag tag_;
24880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    uint8_t* const buf_;
24890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const size_t width_;
2490cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    JDWP::JdwpError error_;
24910399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
249200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
249300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2494a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2495221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2496221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2497221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2498cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return error;
2499221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2500cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  // TODO check thread is suspended by the debugger ?
2501700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
25029837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  GetLocalVisitor visitor(soa, thread, context.get(), frame_id, slot, tag, buf, width);
25030399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2504cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  return visitor.error_;
25050399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
25060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
2507cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien HertzJDWP::JdwpError Dbg::SetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot,
2508cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                   JDWP::JdwpTag tag, uint64_t value, size_t width) {
25090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SetLocalVisitor : public StackVisitor {
25107a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    SetLocalVisitor(Thread* thread, Context* context,
25110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                    JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint64_t value,
2512ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers                    size_t width)
2513b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
25147a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context),
2515cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz          frame_id_(frame_id), slot_(slot), tag_(tag), value_(value), width_(width),
2516cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz          error_(JDWP::ERR_NONE) {}
2517ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
251800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
251900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
252000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
25210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetFrameId() != frame_id_) {
25220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        return true;  // Not our frame, carry on.
2523ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes      }
25240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // TODO: check that the tag is compatible with the actual type of the slot!
2525cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      // TODO: check slot is valid for this method or return INVALID_SLOT error.
2526ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* m = GetMethod();
2527cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      if (m->IsNative()) {
2528cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        // We can't read local value from native method.
2529cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        error_ = JDWP::ERR_OPAQUE_FRAME;
2530cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        return false;
2531cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      }
25320399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      uint16_t reg = DemangleSlot(slot_, m);
25330bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz      constexpr JDWP::JdwpError kFailureErrorCode = JDWP::ERR_ABSENT_INFORMATION;
25340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      switch (tag_) {
25350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_BOOLEAN:
25360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_BYTE:
25370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
25380bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          if (!SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg)) {
25390bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to set boolean/byte local " << reg << " = "
25400bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz                       << static_cast<uint32_t>(value_);
25410bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
25420bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          }
25430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
25440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_SHORT:
25450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_CHAR:
25460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 2U);
25470bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          if (!SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg)) {
25480bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to set short/char local " << reg << " = "
25490bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz                       << static_cast<uint32_t>(value_);
25500bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
25510bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          }
25520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
25530399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_INT:
25542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 4U);
25550bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          if (!SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg)) {
25560bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to set int local " << reg << " = "
25570bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz                       << static_cast<uint32_t>(value_);
25580bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
25590bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          }
25602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          break;
25610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_FLOAT:
25620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 4U);
25630bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          if (!SetVReg(m, reg, static_cast<uint32_t>(value_), kFloatVReg)) {
25640bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to set float local " << reg << " = "
25650bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz                       << static_cast<uint32_t>(value_);
25660bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
25670bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          }
25680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
25690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_ARRAY:
25700bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_CLASS_LOADER:
25710bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_CLASS_OBJECT:
25720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_OBJECT:
25730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_STRING:
25740bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_THREAD:
25750bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_THREAD_GROUP: {
25760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
25772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value_));
257864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes          if (o == ObjectRegistry::kInvalidObject) {
25790bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << tag_ << " object " << o << " is an invalid object";
25800bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = JDWP::ERR_INVALID_OBJECT;
25810bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          } else if (!SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)),
25820bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz                              kReferenceVReg)) {
25830bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            VLOG(jdwp) << "failed to set " << tag_ << " object local " << reg << " = " << o;
25840bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
25850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
25860bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          break;
25870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
25880bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_DOUBLE: {
25892bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 8U);
2590c901dd7bdc80b953d04100ef2f54b8d1ca5f466bSebastien Hertz          bool success = SetVRegPair(m, reg, value_, kDoubleLoVReg, kDoubleHiVReg);
25910bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          if (!success) {
2592c901dd7bdc80b953d04100ef2f54b8d1ca5f466bSebastien Hertz            VLOG(jdwp) << "failed to set double local " << reg << " = " << value_;
25930bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
25940bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          }
25952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          break;
25960bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        }
25970bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        case JDWP::JT_LONG: {
25980399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 8U);
2599c901dd7bdc80b953d04100ef2f54b8d1ca5f466bSebastien Hertz          bool success = SetVRegPair(m, reg, value_, kLongLoVReg, kLongHiVReg);
26000bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          if (!success) {
2601c901dd7bdc80b953d04100ef2f54b8d1ca5f466bSebastien Hertz            VLOG(jdwp) << "failed to set double local " << reg << " = " << value_;
26020bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz            error_ = kFailureErrorCode;
26030bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz          }
26040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
26050bcb2902ec21393d71c94e63aa6733cb5311a0ccSebastien Hertz        }
26060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        default:
26070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          LOG(FATAL) << "Unknown tag " << tag_;
26080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
26090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
26100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return false;
2611cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    }
26120399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
26130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::FrameId frame_id_;
26140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const int slot_;
26150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::JdwpTag tag_;
26160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const uint64_t value_;
26170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const size_t width_;
2618cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    JDWP::JdwpError error_;
26190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
262000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
262100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2622a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2623221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2624221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2625221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2626cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return error;
2627221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2628cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  // TODO check thread is suspended by the debugger ?
2629700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
26307a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  SetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, value, width);
26310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2632cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  return visitor.error_;
2633872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2634872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
26353f52eafe5577b8489f90dc8ed5981b3455206147Sebastien HertzJDWP::ObjectId Dbg::GetThisObjectIdForEvent(mirror::Object* this_object) {
26363f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // If 'this_object' isn't already in the registry, we know that we're not looking for it, so
26373f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // there's no point adding it to the registry and burning through ids.
26383f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // When registering an event request with an instance filter, we've been given an existing object
26393f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // id so it must already be present in the registry when the event fires.
26403f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = 0;
26413f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (this_object != nullptr && gRegistry->Contains(this_object)) {
26423f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    this_id = gRegistry->Add(this_object);
26433f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
26443f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  return this_id;
26453f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
26463f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2647ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersvoid Dbg::PostLocationEvent(mirror::ArtMethod* m, int dex_pc, mirror::Object* this_object,
2648579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                            int event_flags, const JValue* return_value) {
26493f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
26503f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
26513f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
26523f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
26533f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK_EQ(m->IsStatic(), this_object == nullptr);
265491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  JDWP::JdwpLocation location;
2655cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  SetLocation(location, m, dex_pc);
265691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
26573f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // We need 'this' for InstanceOnly filters only.
26583f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = GetThisObjectIdForEvent(this_object);
2659579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  gJdwpState->PostLocationEvent(&location, this_id, event_flags, return_value);
2660872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2661872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
26623f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostFieldAccessEvent(mirror::ArtMethod* m, int dex_pc,
26633f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                               mirror::Object* this_object, mirror::ArtField* f) {
26643f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
26653f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
26663f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
26673f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
26683f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(f != nullptr);
26693f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::JdwpLocation location;
26703f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  SetLocation(location, m, dex_pc);
26713f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
26723f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::RefTypeId type_id = gRegistry->AddRefType(f->GetDeclaringClass());
26733f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::FieldId field_id = ToFieldId(f);
26743f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = gRegistry->Add(this_object);
26753f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
26763f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  gJdwpState->PostFieldEvent(&location, type_id, field_id, this_id, nullptr, false);
26773f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
26783f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
26793f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostFieldModificationEvent(mirror::ArtMethod* m, int dex_pc,
26803f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                     mirror::Object* this_object, mirror::ArtField* f,
26813f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                     const JValue* field_value) {
26823f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
26833f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
26843f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
26853f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
26863f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(f != nullptr);
26873f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(field_value != nullptr);
26883f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::JdwpLocation location;
26893f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  SetLocation(location, m, dex_pc);
26903f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
26913f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::RefTypeId type_id = gRegistry->AddRefType(f->GetDeclaringClass());
26923f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::FieldId field_id = ToFieldId(f);
26933f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = gRegistry->Add(this_object);
26943f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
26953f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  gJdwpState->PostFieldEvent(&location, type_id, field_id, this_id, field_value, true);
26963f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
26973f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
26983f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostException(const ThrowLocation& throw_location,
2699ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                        mirror::ArtMethod* catch_method,
270064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes                        uint32_t catch_dex_pc, mirror::Throwable* exception_object) {
2701c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
27020ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers    return;
27030ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers  }
27044740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
270562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  JDWP::JdwpLocation jdwp_throw_location;
270662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SetLocation(jdwp_throw_location, throw_location.GetMethod(), throw_location.GetDexPc());
2707d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  JDWP::JdwpLocation catch_location;
2708caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  SetLocation(catch_location, catch_method, catch_dex_pc);
2709d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
27103f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // We need 'this' for InstanceOnly filters only.
27113f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = GetThisObjectIdForEvent(throw_location.GetThis());
271264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::ObjectId exception_id = gRegistry->Add(exception_object);
271364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId exception_class_id = gRegistry->AddRefType(exception_object->GetClass());
2714d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
271562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  gJdwpState->PostException(&jdwp_throw_location, exception_id, exception_class_id, &catch_location,
271662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                            this_id);
2717872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2718872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
27192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::PostClassPrepare(mirror::Class* c) {
2720c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
27214740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes    return;
27224740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  }
27234740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
27243d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes  // OLD-TODO - we currently always send both "verified" and "prepared" since
27254740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  // debuggers seem to like that.  There might be some advantage to honesty,
27264740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  // since the class may not yet be verified.
27274740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
27284d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag tag = GetTypeTag(c);
2729f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  gJdwpState->PostClassPrepare(tag, gRegistry->Add(c), c->GetDescriptor(), state);
2730872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2731872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
273262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object,
27338379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz                         mirror::ArtMethod* m, uint32_t dex_pc,
27348379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz                         int event_flags, const JValue* return_value) {
273562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) {
27362aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes    return;
273791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
273891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
27398696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  if (IsBreakpoint(m, dex_pc)) {
27408696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    event_flags |= kBreakpoint;
274191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
274291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
274361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // If the debugger is single-stepping one of our threads, check to
274461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // see if we're that thread and we've reached a step point.
274561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  const SingleStepControl* single_step_control = thread->GetSingleStepControl();
274661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
274761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (single_step_control->is_active) {
274861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    CHECK(!m->IsNative());
274961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    if (single_step_control->step_depth == JDWP::SD_INTO) {
275061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step into method calls.  We break when the line number
275161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // or method pointer changes.  If we're in SS_MIN mode, we
275261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // always stop.
275361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (single_step_control->method != m) {
275461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
275561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new method";
275661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (single_step_control->step_size == JDWP::SS_MIN) {
275761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
275861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new instruction";
2759bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      } else if (single_step_control->ContainsDexPc(dex_pc)) {
276061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
276161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new line";
276261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
276361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else if (single_step_control->step_depth == JDWP::SD_OVER) {
276461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step over method calls.  We break when the line number is
276561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // different and the frame depth is <= the original frame
276661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth.  (We can't just compare on the method, because we
276761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // might get unrolled past it by an exception, and it's tricky
276861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // to identify recursion.)
276961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
277061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
277161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
277261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
277361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Popped up one or more frames, always trigger.
277461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
277561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
277661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (stack_depth == single_step_control->stack_depth) {
277761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Same depth, see if we moved.
277861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control->step_size == JDWP::SS_MIN) {
27798696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          event_flags |= kSingleStep;
27808696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          VLOG(jdwp) << "SS new instruction";
2781bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz        } else if (single_step_control->ContainsDexPc(dex_pc)) {
27822435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          event_flags |= kSingleStep;
27832435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          VLOG(jdwp) << "SS new line";
278491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes        }
278561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
278661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else {
278761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      CHECK_EQ(single_step_control->step_depth, JDWP::SD_OUT);
278861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Return from the current method.  We break when the frame
278961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth pops up.
279061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
279161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // This differs from the "method exit" break in that it stops
279261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // with the PC at the next instruction in the returned-to
279361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // function, rather than the end of the returning function.
279461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
279561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
279661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
279761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
279861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
27998696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
280091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    }
280191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
280291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
280391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // If there's something interesting going on, see if it matches one
280491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // of the debugger filters.
280591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (event_flags != 0) {
28068379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value);
280791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
280891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes}
280991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
281042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) {
281142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz  switch (instrumentation_event) {
281242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kMethodEntered:
281342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &method_enter_event_ref_count_;
281442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kMethodExited:
281542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &method_exit_event_ref_count_;
281642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kDexPcMoved:
281742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &dex_pc_change_event_ref_count_;
281842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kFieldRead:
281942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &field_read_event_ref_count_;
282042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kFieldWritten:
282142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &field_write_event_ref_count_;
282242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kExceptionCaught:
282342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &exception_catch_event_ref_count_;
282442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    default:
282542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return nullptr;
282642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz  }
282742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz}
282842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz
28294d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz// Process request while all mutator threads are suspended.
28304d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) {
2831138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
28320ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  switch (request.GetKind()) {
28334d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kNothing:
28344d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      LOG(WARNING) << "Ignoring empty deoptimization request.";
28354d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
283642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kRegisterForEvent:
283742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x",
28380ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                 request.InstrumentationEvent());
28390ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation->AddListener(&gDebugInstrumentationListener, request.InstrumentationEvent());
28400ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation_events_ |= request.InstrumentationEvent();
284142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
284242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kUnregisterForEvent:
284342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x",
28440ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                 request.InstrumentationEvent());
284542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      instrumentation->RemoveListener(&gDebugInstrumentationListener,
28460ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                      request.InstrumentationEvent());
28470ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation_events_ &= ~request.InstrumentationEvent();
284842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
28494d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullDeoptimization:
28507ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize the world ...";
28514d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->DeoptimizeEverything();
28527ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize the world DONE";
28534d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
28544d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullUndeoptimization:
28557ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize the world ...";
28564d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->UndeoptimizeEverything();
28577ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize the world DONE";
28584d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
28594d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveDeoptimization:
28600ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " ...";
28610ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation->Deoptimize(request.Method());
28620ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.Method()) << " DONE";
28634d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
28644d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveUndeoptimization:
28650ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " ...";
28660ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      instrumentation->Undeoptimize(request.Method());
28670ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.Method()) << " DONE";
28684d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
28694d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    default:
28700ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      LOG(FATAL) << "Unsupported deoptimization request kind " << request.GetKind();
28714d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
28724d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
28734d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz}
28744d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz
28757ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::DelayFullUndeoptimization() {
28767ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  MutexLock mu(Thread::Current(), *deoptimization_lock_);
28777ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  ++delayed_full_undeoptimization_count_;
28787ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  DCHECK_LE(delayed_full_undeoptimization_count_, full_deoptimization_event_count_);
28797ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
28807ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
28817ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::ProcessDelayedFullUndeoptimizations() {
28827ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  // TODO: avoid taking the lock twice (once here and once in ManageDeoptimization).
28837ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  {
28847ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    MutexLock mu(Thread::Current(), *deoptimization_lock_);
28857ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    while (delayed_full_undeoptimization_count_ > 0) {
28867ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      DeoptimizationRequest req;
28870ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req.SetKind(DeoptimizationRequest::kFullUndeoptimization);
28880ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req.SetMethod(nullptr);
28897ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      RequestDeoptimizationLocked(req);
28907ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      --delayed_full_undeoptimization_count_;
28917ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    }
28927ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  }
28937ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  ManageDeoptimization();
28947ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
28957ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
28964d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::RequestDeoptimization(const DeoptimizationRequest& req) {
28970ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  if (req.GetKind() == DeoptimizationRequest::kNothing) {
28984d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    // Nothing to do.
28994d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    return;
29004d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
29014d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  MutexLock mu(Thread::Current(), *deoptimization_lock_);
29027ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  RequestDeoptimizationLocked(req);
29037ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
29047ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
29057ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) {
29060ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  switch (req.GetKind()) {
290742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kRegisterForEvent: {
29080ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK_NE(req.InstrumentationEvent(), 0u);
29090ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent());
291042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x",
29110ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                                req.InstrumentationEvent());
291242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      if (*counter == 0) {
29137d2ae437a87ceb2bdda098ab11f4da588c6a75f5Sebastien Hertz        VLOG(jdwp) << StringPrintf("Queue request #%zd to start listening to instrumentation event 0x%x",
29140ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                   deoptimization_requests_.size(), req.InstrumentationEvent());
291542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz        deoptimization_requests_.push_back(req);
291642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      }
291742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      *counter = *counter + 1;
291842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
291942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    }
292042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kUnregisterForEvent: {
29210ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK_NE(req.InstrumentationEvent(), 0u);
29220ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      size_t* counter = GetReferenceCounterForEvent(req.InstrumentationEvent());
292342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x",
29240ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                                req.InstrumentationEvent());
292542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      *counter = *counter - 1;
292642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      if (*counter == 0) {
29277d2ae437a87ceb2bdda098ab11f4da588c6a75f5Sebastien Hertz        VLOG(jdwp) << StringPrintf("Queue request #%zd to stop listening to instrumentation event 0x%x",
29280ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                                   deoptimization_requests_.size(), req.InstrumentationEvent());
292942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz        deoptimization_requests_.push_back(req);
293042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      }
293142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
293242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    }
29334d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullDeoptimization: {
29340ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK(req.Method() == nullptr);
29354d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      if (full_deoptimization_event_count_ == 0) {
29367ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz        VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
29377ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                   << " for full deoptimization";
29384d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz        deoptimization_requests_.push_back(req);
2939138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      }
29404d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      ++full_deoptimization_event_count_;
29414d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
29424d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
29434d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullUndeoptimization: {
29440ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK(req.Method() == nullptr);
2945e713d9338ad122d6b8c7997387d0c9fc464eea3eSebastien Hertz      DCHECK_GT(full_deoptimization_event_count_, 0U);
29464d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      --full_deoptimization_event_count_;
29474d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      if (full_deoptimization_event_count_ == 0) {
29487ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz        VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
29497ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                   << " for full undeoptimization";
29504d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz        deoptimization_requests_.push_back(req);
2951138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      }
29524d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
29534d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
29544d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveDeoptimization: {
29550ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK(req.Method() != nullptr);
29567ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
29570ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                 << " for deoptimization of " << PrettyMethod(req.Method());
29584d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.push_back(req);
29594d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
29604d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
29614d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveUndeoptimization: {
29620ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      DCHECK(req.Method() != nullptr);
29637ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
29640ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi                 << " for undeoptimization of " << PrettyMethod(req.Method());
29654d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.push_back(req);
29664d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
29674d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
29684d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    default: {
29690ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      LOG(FATAL) << "Unknown deoptimization request kind " << req.GetKind();
29704d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
2971138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2972138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2973138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz}
2974138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2975138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertzvoid Dbg::ManageDeoptimization() {
2976138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  Thread* const self = Thread::Current();
2977138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  {
2978138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    // Avoid suspend/resume if there is no pending request.
29794d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(self, *deoptimization_lock_);
29804d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    if (deoptimization_requests_.empty()) {
2981138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      return;
2982138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2983138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2984138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  CHECK_EQ(self->GetState(), kRunnable);
2985138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  self->TransitionFromRunnableToSuspended(kWaitingForDeoptimization);
2986138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  // We need to suspend mutator threads first.
2987138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  Runtime* const runtime = Runtime::Current();
2988138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetThreadList()->SuspendAll();
2989138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  const ThreadState old_state = self->SetStateUnsafe(kRunnable);
29904d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  {
29914d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(self, *deoptimization_lock_);
29927ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    size_t req_index = 0;
29930ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    for (DeoptimizationRequest& request : deoptimization_requests_) {
29947ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Process deoptimization request #" << req_index++;
29954d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      ProcessDeoptimizationRequest(request);
29964d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
29974d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    deoptimization_requests_.clear();
29984d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
2999138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
3000138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetThreadList()->ResumeAll();
3001138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  self->TransitionFromSuspendedToRunnable();
3002138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz}
3003138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3004a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic bool IsMethodPossiblyInlined(Thread* self, mirror::ArtMethod* m)
3005a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3006bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const DexFile::CodeItem* code_item = m->GetCodeItem();
3007a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (code_item == nullptr) {
3008a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // TODO We should not be asked to watch location in a native or abstract method so the code item
3009a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // should never be null. We could just check we never encounter this case.
3010a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    return false;
3011a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  }
3012eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<2> hs(self);
3013bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  mirror::Class* declaring_class = m->GetDeclaringClass();
3014bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  Handle<mirror::DexCache> dex_cache(hs.NewHandle(declaring_class->GetDexCache()));
3015bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(declaring_class->GetClassLoader()));
3016bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  verifier::MethodVerifier verifier(dex_cache->GetDexFile(), &dex_cache, &class_loader,
3017bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                    &m->GetClassDef(), code_item, m->GetDexMethodIndex(), m,
301846960fe5dcc1be07b39a55114338423a73554449Ian Rogers                                    m->GetAccessFlags(), false, true, false);
3019a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  // Note: we don't need to verify the method.
3020a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  return InlineMethodAnalyser::AnalyseMethodCode(&verifier, nullptr);
3021a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
3022138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3023a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic const Breakpoint* FindFirstBreakpointForMethod(mirror::ArtMethod* m)
30240ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
30250ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  for (Breakpoint& breakpoint : gBreakpoints) {
30260ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    if (breakpoint.Method() == m) {
3027a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      return &breakpoint;
3028138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
3029a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  }
3030a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  return nullptr;
3031a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
3032138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3033a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Sanity checks all existing breakpoints on the same method.
3034a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic void SanityCheckExistingBreakpoints(mirror::ArtMethod* m, bool need_full_deoptimization)
3035a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_)  {
3036a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (kIsDebugBuild) {
3037a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    for (const Breakpoint& breakpoint : gBreakpoints) {
30380ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      CHECK_EQ(need_full_deoptimization, breakpoint.NeedFullDeoptimization());
3039a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
3040a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
3041a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // We should have deoptimized everything but not "selectively" deoptimized this method.
3042a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK(Runtime::Current()->GetInstrumentation()->AreAllMethodsDeoptimized());
3043a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK(!Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
3044a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
3045a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // We should have "selectively" deoptimized this method.
3046a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // Note: while we have not deoptimized everything for this method, we may have done it for
3047a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // another event.
3048a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK(Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
3049a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
3050138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3051a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
3052138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
3053a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Installs a breakpoint at the specified location. Also indicates through the deoptimization
3054a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// request if we need to deoptimize.
3055a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzvoid Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
3056a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  Thread* const self = Thread::Current();
3057a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  mirror::ArtMethod* m = FromMethodId(location->method_id);
3058a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  DCHECK(m != nullptr) << "No method for method id " << location->method_id;
3059a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
3060a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  MutexLock mu(self, *Locks::breakpoint_lock_);
3061a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m);
3062a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  bool need_full_deoptimization;
3063a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (existing_breakpoint == nullptr) {
3064a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is no breakpoint on this method yet: we need to deoptimize. If this method may be
3065a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // inlined, we deoptimize everything; otherwise we deoptimize only this method.
3066a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    need_full_deoptimization = IsMethodPossiblyInlined(self, m);
3067a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
30680ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetKind(DeoptimizationRequest::kFullDeoptimization);
30690ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetMethod(nullptr);
3070a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
30710ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetKind(DeoptimizationRequest::kSelectiveDeoptimization);
30720ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetMethod(m);
3073a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
3074a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  } else {
3075a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is at least one breakpoint for this method: we don't need to deoptimize.
30760ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    req->SetKind(DeoptimizationRequest::kNothing);
30770ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    req->SetMethod(nullptr);
3078a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
30790ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    need_full_deoptimization = existing_breakpoint->NeedFullDeoptimization();
3080a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SanityCheckExistingBreakpoints(m, need_full_deoptimization);
3081138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3082a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
3083a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  gBreakpoints.push_back(Breakpoint(m, location->dex_pc, need_full_deoptimization));
3084a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": "
3085a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz             << gBreakpoints[gBreakpoints.size() - 1];
3086872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3087872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3088a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization
3089a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// request if we need to undeoptimize.
30904d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
30910ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
3092ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(location->method_id);
3093a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  DCHECK(m != nullptr) << "No method for method id " << location->method_id;
3094a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  bool need_full_deoptimization = false;
3095a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
30960ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    if (gBreakpoints[i].DexPc() == location->dex_pc && gBreakpoints[i].Method() == m) {
3097a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
30980ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      need_full_deoptimization = gBreakpoints[i].NeedFullDeoptimization();
3099a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      DCHECK_NE(need_full_deoptimization, Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
3100a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      gBreakpoints.erase(gBreakpoints.begin() + i);
3101a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      break;
3102138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
3103138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
3104a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m);
3105a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (existing_breakpoint == nullptr) {
3106a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is no more breakpoint on this method: we need to undeoptimize.
3107a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
3108a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // This method required full deoptimization: we need to undeoptimize everything.
31090ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetKind(DeoptimizationRequest::kFullUndeoptimization);
31100ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetMethod(nullptr);
3111a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
3112a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // This method required selective deoptimization: we need to undeoptimize only that method.
31130ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetKind(DeoptimizationRequest::kSelectiveUndeoptimization);
31140ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi      req->SetMethod(m);
3115a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
3116a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  } else {
3117a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is at least one breakpoint for this method: we don't need to undeoptimize.
31180ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    req->SetKind(DeoptimizationRequest::kNothing);
31190ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi    req->SetMethod(nullptr);
3120a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SanityCheckExistingBreakpoints(m, need_full_deoptimization);
31218696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
3122872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3123872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3124449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't
3125449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// cause suspension if the thread is the current thread.
3126449db33fafa29578df60e8a323f78d5eb6247e76Jeff Haoclass ScopedThreadSuspension {
3127449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao public:
312833e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers  ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id)
312952d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz      LOCKS_EXCLUDED(Locks::thread_list_lock_)
313033e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
3131449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      thread_(NULL),
3132449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_(JDWP::ERR_NONE),
3133449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      self_suspend_(false),
313433e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      other_suspend_(false) {
3135449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    ScopedObjectAccessUnchecked soa(self);
3136449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    {
3137449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
3138449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_ = DecodeThread(soa, thread_id, thread_);
3139449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
3140449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (error_ == JDWP::ERR_NONE) {
3141449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      if (thread_ == soa.Self()) {
3142449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        self_suspend_ = true;
3143449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      } else {
3144449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
3145449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        jobject thread_peer = gRegistry->GetJObject(thread_id);
3146449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        bool timed_out;
3147d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        Thread* suspended_thread = ThreadList::SuspendThreadByPeer(thread_peer, true, true,
3148d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                                                   &timed_out);
3149449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension);
3150449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        if (suspended_thread == NULL) {
3151449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          // Thread terminated from under us while suspending.
3152449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          error_ = JDWP::ERR_INVALID_THREAD;
3153449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        } else {
3154449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          CHECK_EQ(suspended_thread, thread_);
3155449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          other_suspend_ = true;
3156449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        }
3157449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      }
3158449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
3159449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3160449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3161449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* GetThread() const {
3162449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return thread_;
3163449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3164449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3165449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError GetError() const {
3166449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return error_;
3167449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3168449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3169449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ~ScopedThreadSuspension() {
3170449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (other_suspend_) {
3171449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      Runtime::Current()->GetThreadList()->Resume(thread_, true);
3172449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
3173449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3174449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3175449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao private:
3176449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* thread_;
3177449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError error_;
3178449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool self_suspend_;
3179449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool other_suspend_;
3180449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao};
3181449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3182221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size,
318300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::JdwpStepDepth step_depth) {
3184449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* self = Thread::Current();
3185449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ScopedThreadSuspension sts(self, thread_id);
3186449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  if (sts.GetError() != JDWP::ERR_NONE) {
3187449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return sts.GetError();
31882435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
31898696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
31902435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
31912435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Work out what Method* we're in, the current line number, and how deep the stack currently
31922435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // is for step-out.
31932435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
31942435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
31950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SingleStepStackVisitor : public StackVisitor {
319661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    explicit SingleStepStackVisitor(Thread* thread, SingleStepControl* single_step_control,
319761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz                                    int32_t* line_number)
3198b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
319961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        : StackVisitor(thread, NULL), single_step_control_(single_step_control),
320061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          line_number_(line_number) {
320161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      DCHECK_EQ(single_step_control_, thread->GetSingleStepControl());
320261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->method = NULL;
320361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->stack_depth = 0;
32048696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
3205ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
320600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
320700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
320800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
320961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      mirror::ArtMethod* m = GetMethod();
32100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!m->IsRuntimeMethod()) {
321161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        ++single_step_control_->stack_depth;
321261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control_->method == NULL) {
3213ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache();
321461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->method = m;
321561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          *line_number_ = -1;
32162435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          if (dex_cache != NULL) {
32174445a7e3398a6143939168097a3aa275b734504dIan Rogers            const DexFile& dex_file = *dex_cache->GetDexFile();
321861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz            *line_number_ = dex_file.GetLineNumFromPC(m, GetDexPc());
32192435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          }
32208696433d1b3d8ba15288483b777edd888de69135Elliott Hughes        }
32218696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
3222530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
32238696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
322461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
322561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
322661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    int32_t* const line_number_;
32278696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  };
3228449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
322961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* const thread = sts.GetThread();
323061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepControl* const single_step_control = thread->GetSingleStepControl();
323161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
323261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  int32_t line_number = -1;
323361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepStackVisitor visitor(thread, single_step_control, &line_number);
32340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
32358696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
32362435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
32372435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Find the dex_pc values that correspond to the current line, for line-based single-stepping.
32382435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
32392435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
32402435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  struct DebugCallbackContext {
3241bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    explicit DebugCallbackContext(SingleStepControl* single_step_control, int32_t line_number,
3242bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz                                  const DexFile::CodeItem* code_item)
3243bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      : single_step_control_(single_step_control), line_number_(line_number), code_item_(code_item),
324461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        last_pc_valid(false), last_pc(0) {
32452435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
32462435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
324761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) {
32482435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context);
324961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (static_cast<int32_t>(line_number) == context->line_number_) {
32502435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        if (!context->last_pc_valid) {
32512435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          // Everything from this address until the next line change is ours.
32522435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc = address;
32532435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc_valid = true;
32542435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
32552435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Otherwise, if we're already in a valid range for this line,
32562435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // just keep going (shouldn't really happen)...
32577934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      } else if (context->last_pc_valid) {  // and the line number is new
32582435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Add everything from the last entry up until here to the set
32592435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) {
326061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          context->single_step_control_->dex_pcs.insert(dex_pc);
32612435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
32622435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        context->last_pc_valid = false;
32632435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
32647934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return false;  // There may be multiple entries for any given line.
32652435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
32662435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
326761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    ~DebugCallbackContext() {
32682435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      // If the line number was the last in the position table...
32692435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      if (last_pc_valid) {
3270bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz        size_t end = code_item_->insns_size_in_code_units_;
32712435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) {
327261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->dex_pcs.insert(dex_pc);
32732435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
32742435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
32752435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
32762435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
327761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
327861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    const int32_t line_number_;
3279bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    const DexFile::CodeItem* const code_item_;
32802435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    bool last_pc_valid;
32812435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    uint32_t last_pc;
32822435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  };
328361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->dex_pcs.clear();
3284ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  mirror::ArtMethod* m = single_step_control->method;
328561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (!m->IsNative()) {
3286bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    const DexFile::CodeItem* const code_item = m->GetCodeItem();
3287bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    DebugCallbackContext context(single_step_control, line_number, code_item);
3288bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    m->GetDexFile()->DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
3289bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier                                     DebugCallbackContext::Callback, NULL, &context);
32903e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes  }
32912435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
32922435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
32932435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Everything else...
32942435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
32952435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
329661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_size = step_size;
329761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_depth = step_depth;
329861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->is_active = true;
32998696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
33002435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  if (VLOG_IS_ON(jdwp)) {
330161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step thread: " << *thread;
330261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step size: " << single_step_control->step_size;
330361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step depth: " << single_step_control->step_depth;
330461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->method);
330561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current line: " << line_number;
330661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->stack_depth;
33072435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step dex_pc values:";
3308bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    for (uint32_t dex_pc : single_step_control->dex_pcs) {
3309bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      VLOG(jdwp) << StringPrintf(" %#x", dex_pc);
33102435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
33112435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
33122435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
33132435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
3314872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3315872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
331661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertzvoid Dbg::UnconfigureStep(JDWP::ObjectId thread_id) {
331761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
331861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
331961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* thread;
332061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
332187118ed6f3f99e7df33214c277cf200a7b9a7499Sebastien Hertz  if (error == JDWP::ERR_NONE) {
332261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* single_step_control = thread->GetSingleStepControl();
332361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    DCHECK(single_step_control != nullptr);
3324bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    single_step_control->Clear();
332561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  }
3326872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3327872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
332845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughesstatic char JdwpTagToShortyChar(JDWP::JdwpTag tag) {
332945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  switch (tag) {
333045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    default:
333145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag);
333245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
333345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Primitives.
333445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BYTE:    return 'B';
333545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CHAR:    return 'C';
333645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_FLOAT:   return 'F';
333745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_DOUBLE:  return 'D';
333845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_INT:     return 'I';
333945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_LONG:    return 'J';
334045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_SHORT:   return 'S';
334145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_VOID:    return 'V';
334245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BOOLEAN: return 'Z';
334345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
334445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Reference types.
334545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_ARRAY:
334645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_OBJECT:
334745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_STRING:
334845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD:
334945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD_GROUP:
335045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_LOADER:
335145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_OBJECT:
335245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return 'L';
335345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  }
335445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes}
335545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
335688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id,
335788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                  JDWP::RefTypeId class_id, JDWP::MethodId method_id,
335800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  uint32_t arg_count, uint64_t* arg_values,
335900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* arg_types, uint32_t options,
336000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* pResultTag, uint64_t* pResultValue,
336100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::ObjectId* pExceptionId) {
3362d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  ThreadList* thread_list = Runtime::Current()->GetThreadList();
3363d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3364d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  Thread* targetThread = NULL;
3365d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  DebugInvokeReq* req = NULL;
336600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* self = Thread::Current();
3367d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
336800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(self);
336950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
3370221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, targetThread);
3371221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
3372221229cb523f849f165fdafbf9785010963715daElliott Hughes      LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id;
3373221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
3374d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3375d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    req = targetThread->GetInvokeReq();
3376d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (!req->ready) {
3377d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread;
3378d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      return JDWP::ERR_INVALID_THREAD;
3379d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3380d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3381d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3382d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We currently have a bug where we don't successfully resume the
3383d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * target thread if the suspend count is too deep.  We're expected to
3384d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * require one "resume" for each "suspend", but when asked to execute
3385d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a method we have to resume fully and then re-suspend it back to the
3386d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * same level.  (The easiest way to cause this is to type "suspend"
3387d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * multiple times in jdb.)
3388d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
3389d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * It's unclear what this means when the event specifies "resume all"
3390d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * and some threads are suspended more deeply than others.  This is
3391d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a rare problem, so for now we just prevent it from hanging forever
3392d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * by rejecting the method invocation request.  Without this, we will
3393d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * be stuck waiting on a suspended thread.
3394d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
339500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    int suspend_count;
339600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
339750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
339800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      suspend_count = targetThread->GetSuspendCount();
339900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
3400d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (suspend_count > 1) {
3401d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count;
34027934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return JDWP::ERR_THREAD_SUSPENDED;  // Probably not expected here.
3403d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3404d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
34053f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    JDWP::JdwpError status;
34062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id);
340764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (receiver == ObjectRegistry::kInvalidObject) {
34083f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
34093f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
341045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
34112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* thread = gRegistry->Get<mirror::Object*>(thread_id);
341264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (thread == ObjectRegistry::kInvalidObject) {
34133f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
34143f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
341545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // TODO: check that 'thread' is actually a java.lang.Thread!
341645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
34172dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_id, status);
341845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (c == NULL) {
34193f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return status;
34203f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
342145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
3422ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = FromMethodId(method_id);
342345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic() != (receiver == NULL)) {
342445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_INVALID_METHODID;
342545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
342645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic()) {
342745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (m->GetDeclaringClass() != c) {
342845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
342945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
343045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    } else {
343145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
343245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
343345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
343445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
343545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
343645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Check the argument list matches the method.
3437bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    uint32_t shorty_len = 0;
3438bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    const char* shorty = m->GetShorty(&shorty_len);
3439bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    if (shorty_len - 1 != arg_count) {
344045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_ILLEGAL_ARGUMENT;
344145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
34420920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
3443bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier    {
3444bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      StackHandleScope<3> hs(soa.Self());
3445bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      MethodHelper mh(hs.NewHandle(m));
3446bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      HandleWrapper<mirror::Object> h_obj(hs.NewHandleWrapper(&receiver));
3447bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      HandleWrapper<mirror::Class> h_klass(hs.NewHandleWrapper(&c));
3448bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      const DexFile::TypeList* types = m->GetParameterTypeList();
3449bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      for (size_t i = 0; i < arg_count; ++i) {
3450bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) {
34510920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_ILLEGAL_ARGUMENT;
34520920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
34530920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
3454bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        if (shorty[i + 1] == 'L') {
3455bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          // Did we really get an argument of an appropriate reference type?
3456bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          mirror::Class* parameter_type = mh.GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
3457bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i]);
3458bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          if (argument == ObjectRegistry::kInvalidObject) {
3459bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier            return JDWP::ERR_INVALID_OBJECT;
3460bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          }
3461bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          if (argument != NULL && !argument->InstanceOf(parameter_type)) {
3462bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier            return JDWP::ERR_ILLEGAL_ARGUMENT;
3463bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          }
3464bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier
3465bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          // Turn the on-the-wire ObjectId into a jobject.
3466bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]);
3467bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          v.l = gRegistry->GetJObject(arg_values[i]);
3468bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        }
34690920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      }
3470bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      // Update in case it moved.
3471bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier      m = mh.GetMethod();
347245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
347345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
3474d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->receiver = receiver;
3475d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->thread = thread;
3476d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->klass = c;
3477d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->method = m;
3478d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_count = arg_count;
3479d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_values = arg_values;
3480d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->options = options;
3481d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->invoke_needed = true;
3482d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3483d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3484d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // The fact that we've released the thread list lock is a bit risky --- if the thread goes
3485d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // away we're sitting high and dry -- but we must release this before the ResumeAllThreads
3486d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // call, and it's unwise to hold it during WaitForSuspend.
3487d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3488d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
3489d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3490d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We change our (JDWP thread) status, which should be THREAD_RUNNING,
349181ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes     * so we can suspend for a GC if the invoke request causes us to
3492d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * run out of memory.  It's also a good idea to change it before locking
3493d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the invokeReq mutex, although that should never be held for long.
3494d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
349500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
3496d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
34974dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Transferring control to event thread";
3498d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    {
3499d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      MutexLock mu(self, req->lock);
3500d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3501d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
35024dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming all threads";
350300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers        thread_list->UndoDebuggerSuspensions();
3504d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      } else {
35054dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming event thread only";
3506d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes        thread_list->Resume(targetThread, true);
3507d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
3508d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3509d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      // Wait for the request to finish executing.
3510d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      while (req->invoke_needed) {
3511d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz        req->cond.Wait(self);
3512d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
3513d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
35144dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Control has returned from event thread";
3515d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3516d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* wait for thread to re-suspend itself */
3517df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom    SuspendThread(thread_id, false /* request_suspension */);
351800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
3519d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3520d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3521d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  /*
3522d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * Suspend the threads.  We waited for the target thread to suspend
3523d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * itself, so all we need to do is suspend the others.
3524d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   *
3525d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * The suspendAllThreads() call will double-suspend the event thread,
3526d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * so we want to resume the target thread once to keep the books straight.
3527d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   */
3528d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
352900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
35304dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Suspending all threads";
353100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    thread_list->SuspendAllForDebugger();
353200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
35334dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Resuming event thread to balance the count";
3534d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    thread_list->Resume(targetThread, true);
3535d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3536d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3537d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Copy the result.
3538d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pResultTag = req->result_tag;
3539d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (IsPrimitiveTag(req->result_tag)) {
3540f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = req->result_value.GetJ();
3541d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else {
3542f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = gRegistry->Add(req->result_value.GetL());
3543d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3544d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pExceptionId = req->exception;
3545d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  return req->error;
3546872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3547872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3548872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
354900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
3550d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
355181ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // We can be called while an exception is pending. We need
3552d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // to preserve that across the method invocation.
3553eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<4> hs(soa.Self());
3554eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  auto old_throw_this_object = hs.NewHandle<mirror::Object>(nullptr);
3555eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  auto old_throw_method = hs.NewHandle<mirror::ArtMethod>(nullptr);
3556eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  auto old_exception = hs.NewHandle<mirror::Throwable>(nullptr);
355762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  uint32_t old_throw_dex_pc;
35589f1020305292a21fd14a402b189c765a125226abSebastien Hertz  bool old_exception_report_flag;
355962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  {
356062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation old_throw_location;
356162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
3562eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    old_throw_this_object.Assign(old_throw_location.GetThis());
3563eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    old_throw_method.Assign(old_throw_location.GetMethod());
3564eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    old_exception.Assign(old_exception_obj);
356562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_dex_pc = old_throw_location.GetDexPc();
35669f1020305292a21fd14a402b189c765a125226abSebastien Hertz    old_exception_report_flag = soa.Self()->IsExceptionReportedToInstrumentation();
356762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->ClearException();
356862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
3569d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3570d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Translate the method through the vtable, unless the debugger wants to suppress it.
3571eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ArtMethod> m(hs.NewHandle(pReq->method));
3572d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz  if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver != NULL) {
3573eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    mirror::ArtMethod* actual_method = pReq->klass->FindVirtualMethodForVirtualOrInterface(m.Get());
3574eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    if (actual_method != m.Get()) {
3575eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m.Get()) << " to " << PrettyMethod(actual_method);
3576eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      m.Assign(actual_method);
357745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
3578d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3579eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m.Get())
3580d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " receiver=" << pReq->receiver
3581d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " arg_count=" << pReq->arg_count;
3582eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  CHECK(m.Get() != nullptr);
3583d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3584d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  CHECK_EQ(sizeof(jvalue), sizeof(uint64_t));
3585d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3586eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  pReq->result_value = InvokeWithJValues(soa, pReq->receiver, soa.EncodeMethod(m.Get()),
358753b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers                                         reinterpret_cast<jvalue*>(pReq->arg_values));
3588d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
358962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  mirror::Throwable* exception = soa.Self()->GetException(NULL);
359062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->ClearException();
359162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  pReq->exception = gRegistry->Add(exception);
3592bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  pReq->result_tag = BasicTagFromDescriptor(m.Get()->GetShorty());
3593d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (pReq->exception != 0) {
359462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    VLOG(jdwp) << "  JDWP invocation returning with exception=" << exception
359562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        << " " << exception->Dump();
3596f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    pReq->result_value.SetJ(0);
3597d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else if (pReq->result_tag == JDWP::JT_OBJECT) {
3598d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* if no exception thrown, examine object result more closely */
35999837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    JDWP::JdwpTag new_tag = TagFromObject(soa, pReq->result_value.GetL());
3600d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (new_tag != pReq->result_tag) {
36014dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes      VLOG(jdwp) << "  JDWP promoted result from " << pReq->result_tag << " to " << new_tag;
3602d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      pReq->result_tag = new_tag;
3603d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3604d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3605d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3606d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * Register the object.  We don't actually need an ObjectId yet,
3607d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * but we do need to be sure that the GC won't move or discard the
3608d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * object when we switch out of RUNNING.  The ObjectId conversion
3609d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * will add the object to the "do not touch" list.
3610d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
3611d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We can't use the "tracked allocation" mechanism here because
3612d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the object is going to be handed off to a different thread.
3613d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
3614f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    gRegistry->Add(pReq->result_value.GetL());
3615d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3616d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3617eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  if (old_exception.Get() != NULL) {
3618eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
361962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                         old_throw_dex_pc);
3620eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
36219f1020305292a21fd14a402b189c765a125226abSebastien Hertz    soa.Self()->SetExceptionReportedToInstrumentation(old_exception_report_flag);
3622d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3623872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3624872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3625d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes/*
36264b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * "request" contains a full JDWP packet, possibly with multiple chunks.  We
3627f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * need to process each, accumulate the replies, and ship the whole thing
3628f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * back.
3629f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
3630f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * Returns "true" if we have a reply.  The reply buffer is newly allocated,
3631f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * and includes the chunk type/length, followed by the data.
3632f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
36333d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes * OLD-TODO: we currently assume that the request and reply include a single
3634f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * chunk.  If this becomes inconvenient we will need to adapt.
3635f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes */
36364b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesbool Dbg::DdmHandlePacket(JDWP::Request& request, uint8_t** pReplyBuf, int* pReplyLen) {
3637f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  Thread* self = Thread::Current();
3638f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  JNIEnv* env = self->GetJniEnv();
3639f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
36404b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t type = request.ReadUnsigned32("type");
36414b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t length = request.ReadUnsigned32("length");
36424b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
36434b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  // Create a byte[] corresponding to 'request'.
36444b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  size_t request_length = request.size();
36454b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length));
36466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (dataArray.get() == NULL) {
36474b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    LOG(WARNING) << "byte[] allocation failed: " << request_length;
3648f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
3649f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3650f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
36514b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  env->SetByteArrayRegion(dataArray.get(), 0, request_length, reinterpret_cast<const jbyte*>(request.data()));
36524b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  request.Skip(request_length);
3653f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3654f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Run through and find all chunks.  [Currently just find the first.]
36556a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ScopedByteArrayRO contents(env, dataArray.get());
36564b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (length != request_length) {
3657ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%zd)", length, request_length);
3658f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3659f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3660f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3661f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
3662eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
3663eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                                                                 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
36644b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                                                 type, dataArray.get(), 0, length));
3665f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (env->ExceptionCheck()) {
3666f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
3667f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionDescribe();
3668f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
3669f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3670f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3671f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
36726a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (chunk.get() == NULL) {
3673f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3674f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3675f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3676f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  /*
3677f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * Pull the pieces out of the chunk.  We copy the results into a
3678f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * newly-allocated buffer that the caller can free.  We don't want to
3679f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * continue using the Chunk object because nothing has a reference to it.
3680f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
3681f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * We could avoid this by returning type/data/offset/length and having
3682f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * the caller be aware of the object lifetime issues, but that
368381ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes   * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
3684f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * if we have responses for multiple chunks.
3685f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
3686f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * So we're pretty much stuck with copying data around multiple times.
3687f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   */
3688eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
36894b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
3690eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
3691eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
3692f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
36934dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length);
36946a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (length == 0 || replyData.get() == NULL) {
3695f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3696f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3697f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
36984b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  const int kChunkHdrLen = 8;
3699f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  uint8_t* reply = new uint8_t[length + kChunkHdrLen];
3700f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (reply == NULL) {
3701f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
3702f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3703f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3704f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 0, type);
3705f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 4, length);
37066a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
3707f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3708f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyBuf = reply;
3709f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyLen = length + kChunkHdrLen;
3710f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
37114b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length);
3712f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  return true;
3713872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3714872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3715a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::DdmBroadcast(bool connect) {
37164dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
371747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
371847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  Thread* self = Thread::Current();
371950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  if (self->GetState() != kRunnable) {
372050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
372150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    /* try anyway? */
372247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
372347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
372447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  JNIEnv* env = self->GetJniEnv();
372547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
3726eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
3727eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
3728eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            event);
372947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (env->ExceptionCheck()) {
373047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
373147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionDescribe();
373247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionClear();
373347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
373447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
373547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3736872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmConnected() {
3737a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(true);
3738872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3739872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3740872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmDisconnected() {
3741a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(false);
374247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = false;
374347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
374447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
374547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes/*
37468218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes * Send a notification when a thread starts, stops, or changes its name.
374747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes *
374847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * Because we broadcast the full set of threads when the notifications are
374947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * first enabled, it's possible for "thread" to be actively executing.
375047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes */
37518218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
375247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (!gDdmThreadNotification) {
375347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    return;
375447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
375547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
37568218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  if (type == CHUNK_TYPE("THDE")) {
37578218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    uint8_t buf[4];
3758d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Set4BE(&buf[0], t->GetThreadId());
37598218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
37608218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  } else {
37618218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
376200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
3763eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    StackHandleScope<1> hs(soa.Self());
3764eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::String> name(hs.NewHandle(t->GetThreadName(soa)));
3765eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    size_t char_count = (name.Get() != NULL) ? name->GetLength() : 0;
3766eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    const jchar* chars = (name.Get() != NULL) ? name->GetCharArray()->GetData() : NULL;
376747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
376821f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    std::vector<uint8_t> bytes;
3769d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Append4BE(bytes, t->GetThreadId());
3770545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    JDWP::AppendUtf16BE(bytes, chars, char_count);
377121f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
377221f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    Dbg::DdmSendChunk(type, bytes);
377347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
377447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
377547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
377647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::DdmSetThreadNotification(bool enable) {
377700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Enable/disable thread notifications.
377847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = enable;
377947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (enable) {
378000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Suspend the VM then post thread start notifications for all threads. Threads attaching will
378100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // see a suspension in progress and block until that ends. They then post their own start
378200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // notification.
378300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    SuspendVM();
378400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    std::list<Thread*> threads;
378550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
378600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
378750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu(self, *Locks::thread_list_lock_);
378800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      threads = Runtime::Current()->GetThreadList()->GetList();
378900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
379000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
379150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      ScopedObjectAccess soa(self);
379202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (Thread* thread : threads) {
379302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR"));
379400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
379500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
379600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ResumeVM();
379747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
379847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
379947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3800a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
3801c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (IsDebuggerActive()) {
3802dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier    ScopedObjectAccessUnchecked soa(Thread::Current());
3803cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers    JDWP::ObjectId id = gRegistry->Add(t->GetPeer());
38048218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR"));
380547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
38068218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  Dbg::DdmSendThreadNotification(t, type);
380747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
380847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
380947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadStart(Thread* t) {
3810a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
381147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
381247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
381347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadDeath(Thread* t) {
3814a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
3815872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3816872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
38178218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) {
38183bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(buf != NULL);
38193bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  iovec vec[1];
38203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
38213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_len = byte_count;
38223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Dbg::DdmSendChunkV(type, vec, 1);
3823872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3824872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
382521f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) {
382621f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  DdmSendChunk(type, bytes.size(), &bytes[0]);
382721f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes}
382821f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes
3829f52935278fca8c7aa220543eef4544e3d1105d91Brian Carlstromvoid Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
38303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (gJdwpState == NULL) {
38314dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type;
38323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
3833cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    gJdwpState->DdmSendChunkV(type, iov, iov_count);
38343bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
3835872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3836872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3837767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesint Dbg::DdmHandleHpifChunk(HpifWhen when) {
3838767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when == HPIF_WHEN_NOW) {
38397162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(when);
3840767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return true;
3841767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3842767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3843767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
3844767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
3845767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3846767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3847767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3848767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  gDdmHpifWhen = when;
3849767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
3850767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3851767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3852767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesbool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
3853767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
3854767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
3855767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3856767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3857767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3858767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
3859767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
3860767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3861767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3862767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3863767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (native) {
3864767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhen = when;
3865767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhat = what;
3866767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  } else {
3867767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhen = when;
3868767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhat = what;
3869767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3870767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
3871767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3872767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
38737162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesvoid Dbg::DdmSendHeapInfo(HpifWhen reason) {
38747162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  // If there's a one-shot 'when', reset it.
38757162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  if (reason == gDdmHpifWhen) {
38767162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
38777162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes      gDdmHpifWhen = HPIF_WHEN_NEVER;
38787162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    }
38797162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  }
38807162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes
38817162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  /*
38827162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Chunk HPIF (client --> server)
38837162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
38847162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Heap Info. General information about the heap,
38857162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * suitable for a summary display.
38867162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
38877162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   [u4]: number of heaps
38887162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
38897162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   For each heap:
38907162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: heap ID
38917162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u8]: timestamp in ms since Unix epoch
38927162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u1]: capture reason (same as 'when' value from server)
38937162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: max heap size in bytes (-Xmx)
38947162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current heap size in bytes
38957162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of bytes allocated
38967162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of objects allocated
38977162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   */
38987162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  uint8_t heap_count = 1;
38991d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::Heap* heap = Runtime::Current()->GetHeap();
390021f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  std::vector<uint8_t> bytes;
3901545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append4BE(bytes, heap_count);
39027934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, 1);  // Heap id (bogus; we only have one heap).
3903545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append8BE(bytes, MilliTime());
3904545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append1BE(bytes, reason);
39057934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetMaxMemory());  // Max allowed heap size in bytes.
39067934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetTotalMemory());  // Current heap size in bytes.
3907b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetBytesAllocated());
3908b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetObjectsAllocated());
390921f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
391021f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes);
3911767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3912767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
39136a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgSolidity {
39146a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FREE = 0,
39156a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_HARD = 1,
39166a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SOFT = 2,
39176a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_WEAK = 3,
39186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_PHANTOM = 4,
39196a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FINALIZABLE = 5,
39206a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SWEEP = 6,
39216a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
39226a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39236a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgKind {
39246a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_OBJECT = 0,
39256a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_CLASS_OBJECT = 1,
39266a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_1 = 2,
39276a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_2 = 3,
39286a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_4 = 4,
39296a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_8 = 5,
39306a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_UNKNOWN = 6,
39316a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_NATIVE = 7,
39326a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
39336a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39346a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_PARTIAL (1<<7)
39356a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
39366a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
393730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogersclass HeapChunkContext {
393830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers public:
39396a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Maximum chunk size.  Obtain this from the formula:
39406a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
39416a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  HeapChunkContext(bool merge, bool native)
394230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      : buf_(16384 - 16),
394330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        type_(0),
394430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        merge_(merge) {
39456a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
39466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    if (native) {
394730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = CHUNK_TYPE("NHSG");
39486a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    } else {
394930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
39506a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
39516a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
39526a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39536a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ~HeapChunkContext() {
395430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (p_ > &buf_[0]) {
39556a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      Flush();
39566a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
39576a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
39586a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39596a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void EnsureHeader(const void* chunk_ptr) {
396030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (!needHeader_) {
39616a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      return;
39626a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
39636a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39646a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Start a new HPSx chunk.
39657934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 1);  // Heap id (bogus; we only have one heap).
39667934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write1BE(&p_, 8);  // Size of allocation unit, in bytes.
39676a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39687934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr));  // virtual address of segment start.
39697934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 0);  // offset of this piece (relative to the virtual address).
39706a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // [u4]: length of piece, in allocation units
39716a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // We won't know this until we're done, so save the offset and stuff in a dummy value.
397230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = p_;
397330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Write4BE(&p_, 0x55555555);
397430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = false;
39756a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
39766a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3977b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3978d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    if (pieceLenField_ == NULL) {
3979d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
3980d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      CHECK(needHeader_);
3981d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      return;
3982d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    }
39836a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Patch the "length of piece" field.
398430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(&buf_[0], pieceLenField_);
398530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(pieceLenField_, p_);
398630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Set4BE(pieceLenField_, totalAllocationUnits_);
39876a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
398830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]);
39896a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
39906a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
39916a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
399200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg)
3993b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3994b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
399530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes);
3996a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
3997a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
39986a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes private:
3999a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  enum { ALLOCATION_UNIT_SIZE = 8 };
4000a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
40016a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void Reset() {
400230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    p_ = &buf_[0];
400315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    startOfNextMemoryChunk_ = NULL;
400430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    totalAllocationUnits_ = 0;
400530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = true;
400630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = NULL;
40076a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
40086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
400900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void HeapChunkCallback(void* start, void* /*end*/, size_t used_bytes)
4010b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
4011b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
401230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
401330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // in the following code not to allocate memory, by ensuring buf_ is of the correct size
401415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (used_bytes == 0) {
401515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start == NULL) {
401615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            // Reset for start of new heap.
401715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
401815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
401915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
402015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Only process in use memory so that free region information
402115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // also includes dlmalloc book keeping.
4022a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
4023a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
40246a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
402515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    /* If we're looking at the native heap, we'll just return
402615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     * (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks
402715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     */
402815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bool native = type_ == CHUNK_TYPE("NHSG");
402915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
403015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (startOfNextMemoryChunk_ != NULL) {
403115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Transmit any pending free memory. Native free memory of
403215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // over kMaxFreeLen could be because of the use of mmaps, so
403315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // don't report. If not free memory then start a new segment.
403415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        bool flush = true;
403515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start > startOfNextMemoryChunk_) {
403615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            const size_t kMaxFreeLen = 2 * kPageSize;
403715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeStart = startOfNextMemoryChunk_;
403815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeEnd = start;
40392d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom            size_t freeLen = reinterpret_cast<char*>(freeEnd) - reinterpret_cast<char*>(freeStart);
404015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            if (!native || freeLen < kMaxFreeLen) {
404115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), freeStart, freeLen);
404215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                flush = false;
404315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            }
404415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
404515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (flush) {
404615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
404715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
404815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
404915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
4050ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    mirror::Object* obj = reinterpret_cast<mirror::Object*>(start);
4051a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
4052a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Determine the type of this chunk.
4053a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
4054a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If it's the same, we should combine them.
405515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    uint8_t state = ExamineObject(obj, native);
405615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
405715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // allocation then the first sizeof(size_t) may belong to it.
405815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    const size_t dlMallocOverhead = sizeof(size_t);
405915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    AppendChunk(state, start, used_bytes + dlMallocOverhead);
40602d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom    startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + dlMallocOverhead;
406115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  }
406215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
406315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void AppendChunk(uint8_t state, void* ptr, size_t length)
4064b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
406515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // Make sure there's enough room left in the buffer.
406615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
406715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // 17 bytes for any header.
406815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t needed = (((length/ALLOCATION_UNIT_SIZE + 255) / 256) * 2) + 17;
406915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
407015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
407115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      Flush();
407215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
4073a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
407415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
407515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
407615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
407715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers          << needed << " bytes)";
407815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return;
407915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
408015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    EnsureHeader(ptr);
4081a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Write out the chunk description.
408215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    length /= ALLOCATION_UNIT_SIZE;   // Convert to allocation units.
408315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    totalAllocationUnits_ += length;
408415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    while (length > 256) {
408530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = state | HPSG_PARTIAL;
408630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = 255;     // length - 1
408715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      length -= 256;
4088a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
408930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    *p_++ = state;
409015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    *p_++ = length - 1;
40916a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
40926a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4093ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint8_t ExamineObject(mirror::Object* o, bool is_native_heap)
4094ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
4095a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (o == NULL) {
4096a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_FREE, 0);
4097a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
40986a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4099a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // It's an allocated chunk. Figure out what it is.
41006a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4101a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If we're looking at the native heap, we'll just return
4102a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks.
410300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (is_native_heap) {
4104a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
41056a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
41066a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41075bfa60ffcc953340feb711ed05cf576ac821905eIan Rogers    if (!Runtime::Current()->GetHeap()->IsLiveObjectLocked(o)) {
410815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
410900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
411000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
41112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = o->GetClass();
4112a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c == NULL) {
4113a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      // The object was probably just created but hasn't been initialized yet.
4114a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
4115a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
41166a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4117590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(c)) {
411815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
4119a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
4120a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
41216a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4122a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsClassClass()) {
4123a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
41246a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
41256a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4126a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsArrayClass()) {
4127a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (o->IsObjectArray()) {
4128a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
4129a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
4130a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      switch (c->GetComponentSize()) {
4131a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
4132a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
4133a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
4134a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
4135a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
41366a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
41376a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4138a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
41396a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
41406a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
414130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  std::vector<uint8_t> buf_;
414230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* p_;
414330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* pieceLenField_;
414415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void* startOfNextMemoryChunk_;
414530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  size_t totalAllocationUnits_;
414630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint32_t type_;
414730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool merge_;
414830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool needHeader_;
414930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers
4150a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
4151a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes};
41526a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41536a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesvoid Dbg::DdmSendHeapSegments(bool native) {
41546a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhen when;
41556a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhat what;
41566a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (!native) {
41576a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmHpsgWhen;
41586a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmHpsgWhat;
41596a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  } else {
41606a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmNhsgWhen;
41616a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmNhsgWhat;
41626a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
41636a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (when == HPSG_WHEN_NEVER) {
41646a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    return;
41656a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
41666a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41676a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Figure out what kind of chunks we'll be sending.
41686a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what);
41696a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
41706a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // First, send a heap start chunk.
41716a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  uint8_t heap_id[4];
41727934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Set4BE(&heap_id[0], 1);  // Heap id (bogus; we only have one heap).
41736a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id);
41746a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4175cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Thread* self = Thread::Current();
4176cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
4177cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // To allow the Walk/InspectAll() below to exclusively-lock the
4178cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // mutator lock, temporarily release the shared access to the
4179cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // mutator lock here by transitioning to the suspended state.
4180cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Locks::mutator_lock_->AssertSharedHeld(self);
4181cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  self->TransitionFromRunnableToSuspended(kSuspended);
4182cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
41836a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Send a series of heap segment chunks.
4184a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native);
4185a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (native) {
4186c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris#ifdef USE_DLMALLOC
41871d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context);
4188c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris#else
4189c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris    UNIMPLEMENTED(WARNING) << "Native heap inspection is only supported with dlmalloc";
4190c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris#endif
4191a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
41921d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
41931d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
41941d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
41951d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    for (It cur = spaces.begin(), end = spaces.end(); cur != end; ++cur) {
4196cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      if ((*cur)->IsMallocSpace()) {
4197cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi        (*cur)->AsMallocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
4198b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier      }
4199b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
4200e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    // Walk the large objects, these are not in the AllocSpace.
4201e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
4202a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
42036a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4204cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Shared-lock the mutator lock back.
4205cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  self->TransitionFromSuspendedToRunnable();
4206cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Locks::mutator_lock_->AssertSharedHeld(self);
4207cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
42086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Finally, send a heap end chunk.
42096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id);
4210767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
4211767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4212b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughesstatic size_t GetAllocTrackerMax() {
4213b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#ifdef HAVE_ANDROID_OS
4214b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  // Check whether there's a system property overriding the number of records.
4215b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  const char* propertyName = "dalvik.vm.allocTrackerMax";
4216b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  char allocRecordMaxString[PROPERTY_VALUE_MAX];
4217b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  if (property_get(propertyName, allocRecordMaxString, "") > 0) {
4218b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    char* end;
4219b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    size_t value = strtoul(allocRecordMaxString, &end, 10);
4220b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (*end != '\0') {
42213e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
42223e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- invalid";
4223b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
4224b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
4225b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (!IsPowerOfTwo(value)) {
42263e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
42273e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- not power of two";
4228b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
4229b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
4230b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    return value;
4231b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  }
4232b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#endif
4233b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  return kDefaultNumAllocRecords;
4234b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes}
4235b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes
4236545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::SetAllocTrackingEnabled(bool enabled) {
4237545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (enabled) {
4238b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    {
4239b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
4240b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      if (recent_allocation_records_ == NULL) {
4241b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        alloc_record_max_ = GetAllocTrackerMax();
4242b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        LOG(INFO) << "Enabling alloc tracker (" << alloc_record_max_ << " entries of "
4243b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz            << kMaxAllocRecordStackDepth << " frames, taking "
4244b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz            << PrettySize(sizeof(AllocRecord) * alloc_record_max_) << ")";
4245b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        alloc_record_head_ = alloc_record_count_ = 0;
4246b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        recent_allocation_records_ = new AllocRecord[alloc_record_max_];
4247b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        CHECK(recent_allocation_records_ != NULL);
4248b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      }
4249545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4250fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers    Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints();
4251545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  } else {
4252fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers    Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
4253b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    {
4254b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
42554345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier      LOG(INFO) << "Disabling alloc tracker";
4256b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      delete[] recent_allocation_records_;
4257b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      recent_allocation_records_ = NULL;
42584345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier      type_cache_.Clear();
4259b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    }
4260545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4261545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4262545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
42630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct AllocRecordStackVisitor : public StackVisitor {
42647a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor(Thread* thread, AllocRecord* record)
4265b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
42667a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, NULL), record(record), depth(0) {}
4267545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
426800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
426900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
427000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
4271545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if (depth >= kMaxAllocRecordStackDepth) {
4272530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return false;
4273545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4274ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = GetMethod();
42750399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    if (!m->IsRuntimeMethod()) {
4276b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      record->StackElement(depth)->SetMethod(m);
4277b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      record->StackElement(depth)->SetDexPc(GetDexPc());
4278530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      ++depth;
4279545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4280530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes    return true;
4281545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4282545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4283545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  ~AllocRecordStackVisitor() {
4284545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // Clear out any unused stack trace elements.
4285545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (; depth < kMaxAllocRecordStackDepth; ++depth) {
4286b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      record->StackElement(depth)->SetMethod(nullptr);
4287b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      record->StackElement(depth)->SetDexPc(0);
4288545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4289545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4290545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4291545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  AllocRecord* record;
4292545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t depth;
4293545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
4294545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
42952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::RecordAllocation(mirror::Class* type, size_t byte_count) {
4296545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  Thread* self = Thread::Current();
4297545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  CHECK(self != NULL);
4298545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4299719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  MutexLock mu(self, *alloc_tracker_lock_);
4300545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
4301545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
4302545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4303545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4304545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Advance and clip.
4305719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (++alloc_record_head_ == alloc_record_max_) {
4306719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    alloc_record_head_ = 0;
4307545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4308545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4309545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the basics.
4310719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  AllocRecord* record = &recent_allocation_records_[alloc_record_head_];
4311b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  record->SetType(type);
4312b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  record->SetByteCount(byte_count);
4313b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi  record->SetThinLockId(self->GetThreadId());
4314545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4315545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the stack trace.
43167a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor visitor(self, record);
43170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
4318545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4319719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (alloc_record_count_ < alloc_record_max_) {
4320719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    ++alloc_record_count_;
4321545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4322545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4323545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4324a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// Returns the index of the head element.
4325a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
4326a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We point at the most-recently-written record, so if gAllocRecordCount is 1
4327a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// we want to use the current element.  Take "head+1" and subtract count
4328a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// from it.
4329a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
4330a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We need to handle underflow in our circular buffer, so we add
4331b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes// gAllocRecordMax and then mask it back down.
4332719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::HeadIndex() {
4333719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  return (Dbg::alloc_record_head_ + 1 + Dbg::alloc_record_max_ - Dbg::alloc_record_count_) &
4334719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      (Dbg::alloc_record_max_ - 1);
4335545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4336545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4337545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::DumpRecentAllocations() {
433800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
4339719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  MutexLock mu(soa.Self(), *alloc_tracker_lock_);
4340545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
4341545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    LOG(INFO) << "Not recording tracked allocations";
4342545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
4343545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4344545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4345545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // "i" is the head of the list.  We want to start at the end of the
4346545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // list and move forward to the tail.
4347a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t i = HeadIndex();
4348719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  size_t count = alloc_record_count_;
4349545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4350719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  LOG(INFO) << "Tracked allocations, (head=" << alloc_record_head_ << " count=" << count << ")";
4351545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  while (count--) {
4352545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    AllocRecord* record = &recent_allocation_records_[i];
4353545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4354b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi    LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->ThinLockId(), record->ByteCount())
4355b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi              << PrettyClass(record->Type());
4356545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4357545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
4358b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      AllocRecordStackTraceElement* stack_element = record->StackElement(stack_frame);
4359b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      mirror::ArtMethod* m = stack_element->Method();
4360545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      if (m == NULL) {
4361545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes        break;
4362545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
4363b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      LOG(INFO) << "    " << PrettyMethod(m) << " line " << stack_element->LineNumber();
4364545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4365545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4366545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // pause periodically to help logcat catch up
4367545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if ((count % 5) == 0) {
4368545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      usleep(40000);
4369545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4370545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4371719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    i = (i + 1) & (alloc_record_max_ - 1);
4372545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4373545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4374545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4375545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesclass StringTable {
4376545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes public:
4377545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  StringTable() {
4378545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4379545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
43804345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  void Add(const std::string& str) {
43814345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    table_.insert(str);
43824345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  }
43834345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier
43844345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier  void Add(const char* str) {
43854345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier    table_.insert(str);
4386545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4387545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4388a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t IndexOf(const char* s) const {
438902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    auto it = table_.find(s);
4390a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    if (it == table_.end()) {
4391a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes      LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
4392a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    }
4393a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    return std::distance(table_.begin(), it);
4394545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4395545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4396a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t Size() const {
4397545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return table_.size();
4398545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4399545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4400a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  void WriteTo(std::vector<uint8_t>& bytes) const {
440102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const std::string& str : table_) {
440202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      const char* s = str.c_str();
44036d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      size_t s_len = CountModifiedUtf8Chars(s);
4404700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<uint16_t> s_utf16(new uint16_t[s_len]);
44056d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      ConvertModifiedUtf8ToUtf16(s_utf16.get(), s);
44066d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len);
4407545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4408545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4409545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4410545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes private:
4411a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  std::set<std::string> table_;
4412545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(StringTable);
4413545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
4414545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4415bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartierstatic const char* GetMethodSourceFile(mirror::ArtMethod* method)
4416280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4417bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  DCHECK(method != nullptr);
4418bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier  const char* source_file = method->GetDeclaringClassSourceFile();
4419280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz  return (source_file != nullptr) ? source_file : "";
4420280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz}
4421280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz
4422545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes/*
4423545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The data we send to DDMS contains everything we have recorded.
4424545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4425545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * Message header (all values big-endian):
4426545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) message header len (to allow future expansion); includes itself
4427545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) entry header len
4428545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) stack frame len
4429545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of entries
4430545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (4b) offset to string table from start of message
4431545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of class name strings
4432545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of method name strings
4433545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of source file name strings
4434545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * For each entry:
4435545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (4b) total allocation size
4436221229cb523f849f165fdafbf9785010963715daElliott Hughes *   (2b) thread id
4437545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (2b) allocated object's class name index
4438545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (1b) stack depth
4439545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   For each stack frame:
4440545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method's class name
4441545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method name
4442545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method source file
4443545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) line number, clipped to 32767; -2 if native; -1 if no source
4444545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) class name strings
4445545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) method name strings
4446545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) source file strings
4447545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4448545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * As with other DDM traffic, strings are sent as a 4-byte length
4449545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * followed by UTF-16 data.
4450545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4451545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We send up 16-bit unsigned indexes into string tables.  In theory there
4452b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes * can be (kMaxAllocRecordStackDepth * gAllocRecordMax) unique strings in
4453545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * each table, but in practice there should be far fewer.
4454545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4455545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The chief reason for using a string table here is to keep the size of
4456545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * the DDMS message to a minimum.  This is partly to make the protocol
4457545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * efficient, but also because we have to form the whole thing up all at
4458545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * once in a memory buffer.
4459545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4460545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We use separate string tables for class names, method names, and source
4461545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * files to keep the indexes small.  There will generally be no overlap
4462545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * between the contents of these tables.
4463545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes */
4464545a064aca775ba801790fced3d713d8a87bfc61Elliott HughesjbyteArray Dbg::GetRecentAllocations() {
4465545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (false) {
4466545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    DumpRecentAllocations();
4467545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4468545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
446950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* self = Thread::Current();
447046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  std::vector<uint8_t> bytes;
447146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  {
4472719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    MutexLock mu(self, *alloc_tracker_lock_);
447346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
447446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 1: generate string tables.
447546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
447646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable class_names;
447746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable method_names;
447846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable filenames;
447946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4480719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    int count = alloc_record_count_;
448146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    int idx = HeadIndex();
448246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
448346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
44844345c46b8a927cf13d9bbe38f8cf0593f5de181bMathieu Chartier      class_names.Add(record->Type()->GetDescriptor());
448546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) {
4486b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi        mirror::ArtMethod* m = record->StackElement(i)->Method();
448746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        if (m != NULL) {
4488bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          class_names.Add(m->GetDeclaringClassDescriptor());
4489bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          method_names.Add(m->GetName());
4490bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier          filenames.Add(GetMethodSourceFile(m));
449146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        }
449246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      }
4493545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4494719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      idx = (idx + 1) & (alloc_record_max_ - 1);
449546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    }
449646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4497719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    LOG(INFO) << "allocation records: " << alloc_record_count_;
449846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
449946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
450046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 2: Generate the output and store it in the buffer.
450146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
450246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
450346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) message header len (to allow future expansion); includes itself
450446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) entry header len
450546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) stack frame len
450646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kMessageHeaderLen = 15;
450746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kEntryHeaderLen = 9;
450846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kStackFrameLen = 8;
450946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kMessageHeaderLen);
451046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kEntryHeaderLen);
451146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kStackFrameLen);
451246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
451346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of entries
451446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (4b) offset to string table from start of message
451546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of class name strings
451646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of method name strings
451746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of source file name strings
4518719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    JDWP::Append2BE(bytes, alloc_record_count_);
451946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    size_t string_table_offset = bytes.size();
45207934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Append4BE(bytes, 0);  // We'll patch this later...
452146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, class_names.Size());
452246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, method_names.Size());
452346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, filenames.Size());
452446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4525719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    count = alloc_record_count_;
452646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    idx = HeadIndex();
452746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
452846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // For each entry:
452946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (4b) total allocation size
453046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) thread id
453146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) allocated object's class name index
453246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (1b) stack depth
453346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
453446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      size_t stack_depth = record->GetDepth();
4535f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier      size_t allocated_object_class_name_index =
4536b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi          class_names.IndexOf(record->Type()->GetDescriptor().c_str());
4537b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      JDWP::Append4BE(bytes, record->ByteCount());
4538b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi      JDWP::Append2BE(bytes, record->ThinLockId());
453946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, allocated_object_class_name_index);
454046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append1BE(bytes, stack_depth);
454146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
454246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
454346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // For each stack frame:
454446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method's class name
454546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method name
454646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method source file
454746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) line number, clipped to 32767; -2 if native; -1 if no source
4548b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi        mirror::ArtMethod* m = record->StackElement(stack_frame)->Method();
4549bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor());
4550bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        size_t method_name_index = method_names.IndexOf(m->GetName());
4551bfd9a4378eacaf2dc2bbe05ad48c5164fc93c9feMathieu Chartier        size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m));
455246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, class_name_index);
455346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, method_name_index);
455446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, file_name_index);
4555b5a9e3d1cc1fd66683e43e365afc8c900e2800c4Hiroshi Yamauchi        JDWP::Append2BE(bytes, record->StackElement(stack_frame)->LineNumber());
4556545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
455746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4558719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      idx = (idx + 1) & (alloc_record_max_ - 1);
4559545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4560545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
456146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) class name strings
456246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) method name strings
456346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) source file strings
456446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Set4BE(&bytes[string_table_offset], bytes.size());
456546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    class_names.WriteTo(bytes);
456646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    method_names.WriteTo(bytes);
456746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    filenames.WriteTo(bytes);
4568545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
456950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  JNIEnv* env = self->GetJniEnv();
4570545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  jbyteArray result = env->NewByteArray(bytes.size());
4571545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (result != NULL) {
4572545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
4573545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4574545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  return result;
4575545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4576545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
45770ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchimirror::ArtMethod* DeoptimizationRequest::Method() const {
45780ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  ScopedObjectAccessUnchecked soa(Thread::Current());
45790ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  return soa.DecodeMethod(method_);
45800ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi}
45810ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi
45820ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchivoid DeoptimizationRequest::SetMethod(mirror::ArtMethod* m) {
45830ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  ScopedObjectAccessUnchecked soa(Thread::Current());
45840ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi  method_ = soa.EncodeMethod(m);
45850ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi}
45860ec17d2ddb69d3f5c46ccad62e82c0ffd6219428Hiroshi Yamauchi
4587872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}  // namespace art
4588