debugger.cc revision c4ddc042eaf5232a3f9b111f42af39eeab6e0294
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"
281d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/accounting/card_table-inl.h"
291d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/large_object_space.h"
301d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/space/space-inl.h"
31eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier#include "handle_scope.h"
3264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "jdwp/object_registry.h"
33ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_field-inl.h"
34ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.h"
352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class.h"
362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class_loader.h"
382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
40b0fa5dc7769c1e054032f39de0a3f6d6dd06f8cfIan Rogers#include "mirror/string-inl.h"
412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/throwable.h"
426d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h"
43a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz#include "quick/inline_method_analyser.h"
4453b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers#include "reflection.h"
45a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include "safe_map.h"
4664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "scoped_thread_state_change.h"
476a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#include "ScopedLocalRef.h"
48f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes#include "ScopedPrimitiveArray.h"
49eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier#include "handle_scope-inl.h"
50475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes#include "thread_list.h"
5162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers#include "throw_location.h"
522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "utf.h"
53a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz#include "verifier/method_verifier-inl.h"
54eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "well_known_classes.h"
55475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
563d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#ifdef HAVE_ANDROID_OS
573d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#include "cutils/properties.h"
583d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#endif
593d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom
60872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesnamespace art {
61872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
627934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstromstatic const size_t kMaxAllocRecordStackDepth = 16;  // Max 255.
637934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstromstatic const size_t kDefaultNumAllocRecords = 64*1024;  // Must be a power of 2.
64475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
65545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstruct AllocRecordStackTraceElement {
66ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* method;
670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  uint32_t dex_pc;
68545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
69412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  AllocRecordStackTraceElement() : method(nullptr), dex_pc(0) {
70412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
71412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
72b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  int32_t LineNumber() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return MethodHelper(method).GetLineNumFromDexPC(dex_pc);
74545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
75545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
76545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
77545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstruct AllocRecord {
782dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* type;
79545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t byte_count;
80545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  uint16_t thin_lock_id;
817934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  AllocRecordStackTraceElement stack[kMaxAllocRecordStackDepth];  // Unused entries have NULL method.
82545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
83545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t GetDepth() {
84545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    size_t depth = 0;
85545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    while (depth < kMaxAllocRecordStackDepth && stack[depth].method != NULL) {
86545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      ++depth;
87545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
88545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return depth;
89545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
90412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
9183c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier  void UpdateObjectPointers(IsMarkedCallback* callback, void* arg)
92412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
93412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    if (type != nullptr) {
9483c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier      type = down_cast<mirror::Class*>(callback(type, arg));
95412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    }
96412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
97412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier      mirror::ArtMethod*& m = stack[stack_frame].method;
98412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier      if (m == nullptr) {
99412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier        break;
100412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier      }
10183c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier      m = down_cast<mirror::ArtMethod*>(callback(m, arg));
102412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    }
103412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
104545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
105545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
1068696433d1b3d8ba15288483b777edd888de69135Elliott Hughesstruct Breakpoint {
107a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  // The location of this breakpoint.
108ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* method;
109a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  uint32_t dex_pc;
110a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
111a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  // Indicates whether breakpoint needs full deoptimization or selective deoptimization.
112a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  bool need_full_deoptimization;
113a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
114a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  Breakpoint(mirror::ArtMethod* method, uint32_t dex_pc, bool need_full_deoptimization)
115a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    : method(method), dex_pc(dex_pc), need_full_deoptimization(need_full_deoptimization) {}
1163b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
1173b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  void VisitRoots(RootCallback* callback, void* arg) {
1183b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    if (method != nullptr) {
1193b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier      callback(reinterpret_cast<mirror::Object**>(&method), arg, 0, kRootDebugger);
1203b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    }
1213b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
1228696433d1b3d8ba15288483b777edd888de69135Elliott Hughes};
1238696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
12400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs)
125b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
126229feb7a09317919ee51c06d1c3e715cea25da75Elliott Hughes  os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.method).c_str(), rhs.dex_pc);
1278696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return os;
1288696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
1298696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
1303f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzclass DebugInstrumentationListener FINAL : public instrumentation::InstrumentationListener {
13162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers public:
13262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  DebugInstrumentationListener() {}
13362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual ~DebugInstrumentationListener() {}
13462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1353f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodEntered(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1363f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                     uint32_t dex_pc)
1373f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
13862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
13962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
14062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
14162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
1428379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::UpdateDebugger(thread, this_object, method, 0, Dbg::kMethodEntry, nullptr);
14362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
14462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1453f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodExited(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1463f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc, const JValue& return_value)
1473f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
14862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
14962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
15062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
15162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
1528379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::UpdateDebugger(thread, this_object, method, dex_pc, Dbg::kMethodExit, &return_value);
15362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
15462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1553f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodUnwind(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1563f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc)
1573f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
15862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    // We're not recorded to listen to this kind of event, so complain.
15962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method)
16051db44a194bafc3810a41164a8b39614f10e79dfSebastien Hertz               << " " << dex_pc;
16162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
16262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1633f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void DexPcMoved(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1643f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                  uint32_t new_dex_pc)
1653f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1668379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc, 0, nullptr);
16762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
16862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1693f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void FieldRead(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1703f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                 uint32_t dex_pc, mirror::ArtField* field)
1713f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1723f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostFieldAccessEvent(method, dex_pc, this_object, field);
17362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
1743f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
1753f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void FieldWritten(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1763f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc, mirror::ArtField* field, const JValue& field_value)
1773f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1783f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostFieldModificationEvent(method, dex_pc, this_object, field, &field_value);
1793f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
1803f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
1813f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
1823f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                       mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
1833f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                       mirror::Throwable* exception_object)
1843f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1853f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostException(throw_location, catch_method, catch_dex_pc, exception_object);
1863f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
1873f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
1883f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz private:
1893f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener);
19062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers} gDebugInstrumentationListener;
19162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1924ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes// JDWP is allowed unless the Zygote forbids it.
1934ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesstatic bool gJdwpAllowed = true;
1944ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
195c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line?
1963bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gJdwpConfigured = false;
1973bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
198c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Broken-down JDWP options. (Only valid if IsJdwpConfigured() is true.)
199376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughesstatic JDWP::JdwpOptions gJdwpOptions;
2003bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
2013bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes// Runtime JDWP state.
2023bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic JDWP::JdwpState* gJdwpState = NULL;
2033bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerConnected;  // debugger or DDMS is connected.
2043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerActive;     // debugger is making requests.
2058696433d1b3d8ba15288483b777edd888de69135Elliott Hughesstatic bool gDisposed;           // debugger called VirtualMachine.Dispose, so we should drop the connection.
2063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
20747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesstatic bool gDdmThreadNotification = false;
20847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
209767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes// DDMS GC-related settings.
210767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
211767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
212767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmHpsgWhat;
213767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
214767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmNhsgWhat;
215767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
216719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstatic ObjectRegistry* gRegistry = nullptr;
217475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
218545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes// Recent allocation tracking.
219719d1a33f6569864f529e5a3fff59e7bca97aad0Ian RogersMutex* Dbg::alloc_tracker_lock_ = nullptr;
220719d1a33f6569864f529e5a3fff59e7bca97aad0Ian RogersAllocRecord* Dbg::recent_allocation_records_ = nullptr;  // TODO: CircularBuffer<AllocRecord>
221719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_max_ = 0;
222719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_head_ = 0;
223719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_count_ = 0;
224545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
225138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz// Deoptimization support.
2264d25df3f76f864b7629ac8c0046d46997f293d8dSebastien HertzMutex* Dbg::deoptimization_lock_ = nullptr;
2274d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzstd::vector<DeoptimizationRequest> Dbg::deoptimization_requests_;
2284d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzsize_t Dbg::full_deoptimization_event_count_ = 0;
2297ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzsize_t Dbg::delayed_full_undeoptimization_count_ = 0;
230138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
23142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz// Instrumentation event reference counters.
23242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::dex_pc_change_event_ref_count_ = 0;
23342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::method_enter_event_ref_count_ = 0;
23442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::method_exit_event_ref_count_ = 0;
23542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::field_read_event_ref_count_ = 0;
23642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::field_write_event_ref_count_ = 0;
23742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t Dbg::exception_catch_event_ref_count_ = 0;
23842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzuint32_t Dbg::instrumentation_events_ = 0;
23942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz
240138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz// Breakpoints.
24109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhaostatic std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_);
2428696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2433b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid DebugInvokeReq::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
2443b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier                                RootType root_type) {
2453b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (receiver != nullptr) {
2463b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(&receiver, arg, tid, root_type);
2473b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2483b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (thread != nullptr) {
2493b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(&thread, arg, tid, root_type);
2503b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2513b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (klass != nullptr) {
2523b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&klass), arg, tid, root_type);
2533b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2543b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (method != nullptr) {
2553b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&method), arg, tid, root_type);
2563b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2573b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
2583b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
259bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzvoid DebugInvokeReq::Clear() {
260bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  invoke_needed = false;
261bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  receiver = nullptr;
262bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  thread = nullptr;
263bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  klass = nullptr;
264bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  method = nullptr;
265bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
266bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
2673b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid SingleStepControl::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
2683b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier                                   RootType root_type) {
2693b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (method != nullptr) {
2703b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&method), arg, tid, root_type);
2713b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2723b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
2733b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
274bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzbool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const {
275bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  return dex_pcs.find(dex_pc) == dex_pcs.end();
276bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
277bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
278bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzvoid SingleStepControl::Clear() {
279bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  is_active = false;
280bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  method = nullptr;
281bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  dex_pcs.clear();
282bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
283bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
2844d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid DeoptimizationRequest::VisitRoots(RootCallback* callback, void* arg) {
2854d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  if (method != nullptr) {
2864d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    callback(reinterpret_cast<mirror::Object**>(&method), arg, 0, kRootDebugger);
2874d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
2884d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz}
2894d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz
290ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic bool IsBreakpoint(const mirror::ArtMethod* m, uint32_t dex_pc)
29109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    LOCKS_EXCLUDED(Locks::breakpoint_lock_)
292b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
29309bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
294138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
295a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes    if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == dex_pc) {
2968696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i];
2978696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      return true;
2988696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
2998696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
3008696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return false;
3018696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
3028696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
30352d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertzstatic bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread)
30452d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) {
3059e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
3069e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // A thread may be suspended for GC; in this code, we really want to know whether
3079e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // there's a debugger suspension active.
3089e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0;
3099e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes}
3109e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
3112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status)
312b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3132dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
31464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
315436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
316436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
317436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
318436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsArrayInstance()) {
319436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_ARRAY;
320436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
321436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
322436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
323436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsArray();
324436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
325436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
3262dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError& status)
327b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3282dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
32964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
330436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
331436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
332436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
333436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsClass()) {
334436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_CLASS;
335436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
336436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
337436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
338436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsClass();
339436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
340436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
341221229cb523f849f165fdafbf9785010963715daElliott Hughesstatic JDWP::JdwpError DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, Thread*& thread)
342a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
343b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
344b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3452dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_peer = gRegistry->Get<mirror::Object*>(thread_id);
34664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_peer == NULL || thread_peer == ObjectRegistry::kInvalidObject) {
347221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't even an object.
348221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_OBJECT;
349221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
350221229cb523f849f165fdafbf9785010963715daElliott Hughes
3512dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
352221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) {
353221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't a thread.
354221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_THREAD;
355221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
356221229cb523f849f165fdafbf9785010963715daElliott Hughes
357221229cb523f849f165fdafbf9785010963715daElliott Hughes  thread = Thread::FromManagedThread(soa, thread_peer);
358221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (thread == NULL) {
359221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This is a java.lang.Thread without a Thread*. Must be a zombie.
360221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_THREAD_NOT_ALIVE;
361436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
362221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
363436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
364436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
36524437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) {
36624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // JDWP deliberately uses the descriptor characters' ASCII values for its enum.
36724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // Note that by "basic" we mean that we don't get more specific than JT_OBJECT.
36824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  return static_cast<JDWP::JdwpTag>(descriptor[0]);
36924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
37024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
3719837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogersstatic JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c)
372b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
37386b0010c79ef95b5333cd540b7d3af34a9f1a643Elliott Hughes  CHECK(c != NULL);
37424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsArrayClass()) {
37524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_ARRAY;
37624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
37724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsStringClass()) {
37824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_STRING;
3799837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
3809837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  if (c->IsClassClass()) {
38124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_CLASS_OBJECT;
38224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
3839837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
3849837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* thread_class = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
3859837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (thread_class->IsAssignableFrom(c)) {
3869837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_THREAD;
3879837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
3889837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
3899837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
3909837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* thread_group_class =
3919837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
3929837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (thread_group_class->IsAssignableFrom(c)) {
3939837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_THREAD_GROUP;
3949837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
3959837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
3969837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
3979837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* class_loader_class =
3989837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader);
3999837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (class_loader_class->IsAssignableFrom(c)) {
4009837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_CLASS_LOADER;
4019837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
4029837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
4039837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  return JDWP::JT_OBJECT;
40424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
40524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
40624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes/*
40724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Objects declared to hold Object might actually hold a more specific
40824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * type.  The debugger may take a special interest in these (e.g. it
40924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * wants to display the contents of Strings), so we want to return an
41024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * appropriate tag.
41124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes *
41224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Null objects are tagged JT_OBJECT.
41324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes */
4149837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogersstatic JDWP::JdwpTag TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o)
415b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4169837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass());
41724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
41824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
41924437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic bool IsPrimitiveTag(JDWP::JdwpTag tag) {
42024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  switch (tag) {
42124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BOOLEAN:
42224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BYTE:
42324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_CHAR:
42424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_FLOAT:
42524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_DOUBLE:
42624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_INT:
42724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_LONG:
42824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_SHORT:
42924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_VOID:
43024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return true;
43124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  default:
43224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return false;
43324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
43424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
43524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
4363bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
4373bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Handle one of the JDWP name/value pairs.
4383bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
4393bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * JDWP options are:
4403bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  help: if specified, show help message and bail
4413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  transport: may be dt_socket or dt_shmem
4423bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  address: for dt_socket, "host:port", or just "port" when listening
4433bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  server: if "y", wait for debugger to attach; if "n", attach to debugger
4443bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  timeout: how long to wait for debugger to connect / listen
4453bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
4463bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Useful with server=n (these aren't supported yet):
4473bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onthrow=<exception-name>: connect to debugger when exception thrown
4483bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onuncaught=y|n: connect to debugger when uncaught exception thrown
4493bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  launch=<command-line>: launch the debugger itself
4503bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
4513bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * The "transport" option is required, as is "address" if server=n.
4523bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
4533bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool ParseJdwpOption(const std::string& name, const std::string& value) {
4543bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (name == "transport") {
4553bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "dt_socket") {
456376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
4573bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "dt_android_adb") {
458376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
4593bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
4603bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP transport not supported: " << value;
4613bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4623bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4633bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "server") {
4643bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
465376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = false;
4663bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
467376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = true;
4683bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
4693bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
4703bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4713bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4723bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "suspend") {
4733bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
474376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = false;
4753bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
476376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = true;
4773bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
4783bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
4793bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4803bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4813bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "address") {
4823bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* this is either <port> or <host>:<port> */
4833bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string port_string;
484376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.host.clear();
4853bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type colon = value.find(':');
4863bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (colon != std::string::npos) {
487376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.host = value.substr(0, colon);
4883bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value.substr(colon + 1);
4893bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
4903bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value;
4913bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4923bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (port_string.empty()) {
4933bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address missing port: " << value;
4943bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4953bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4963bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    char* end;
497ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    uint64_t port = strtoul(port_string.c_str(), &end, 10);
498ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    if (*end != '\0' || port > 0xffff) {
4993bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address has junk in port field: " << value;
5003bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5013bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
502376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.port = port;
5033bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
5043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* valid but unsupported */
5053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
5063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
5073bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
5083bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
5093bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5103bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
5113bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
5123bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5133bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
5143bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
5153bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * "transport=dt_socket,address=8000,server=y,suspend=n"
5163bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
5173bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesbool Dbg::ParseJdwpOptions(const std::string& options) {
5184dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "ParseJdwpOptions: " << options;
51947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
5203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  std::vector<std::string> pairs;
5213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Split(options, ',', pairs);
5223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5233bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  for (size_t i = 0; i < pairs.size(); ++i) {
5243bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type equals = pairs[i].find('=');
5253bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (equals == std::string::npos) {
5263bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
5273bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5283bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5293bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
5303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
5313bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
532376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
5333bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP transport: " << options;
5343bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
535376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
5363bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
5373bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    return false;
5383bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
5393bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5403bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gJdwpConfigured = true;
5413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
5423bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
5433bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
544d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StartJdwp() {
545c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!gJdwpAllowed || !IsJdwpConfigured()) {
546376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    // No JDWP for you!
547376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    return;
548376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  }
549376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes
550719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  CHECK(gRegistry == nullptr);
551475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gRegistry = new ObjectRegistry;
552475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
553719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  alloc_tracker_lock_ = new Mutex("AllocTracker lock");
5544d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  deoptimization_lock_ = new Mutex("deoptimization lock", kDeoptimizationLock);
555d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // Init JDWP if the debugger is enabled. This may connect out to a
556d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger, passively listen for a debugger, or block waiting for a
557d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger.
558376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
559376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState == NULL) {
560f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // We probably failed because some other process has the port already, which means that
561f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // if we don't abort the user is likely to think they're talking to us when they're actually
562f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // talking to that other process.
5633d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Debugger thread failed to initialize";
564d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
565d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes
566d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // If a debugger has already attached, send the "welcome" message.
567d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // This may cause us to suspend all threads.
568376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState->IsActive()) {
56900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
570376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    if (!gJdwpState->PostVMStart()) {
5713d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes      LOG(WARNING) << "Failed to post 'start' message to debugger";
572d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes    }
573d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
574872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
575872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
5763b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid Dbg::VisitRoots(RootCallback* callback, void* arg) {
5774d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  {
5784d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
5794d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    for (Breakpoint& bp : gBreakpoints) {
5804d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      bp.VisitRoots(callback, arg);
5814d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
5824d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
5834d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  if (deoptimization_lock_ != nullptr) {  // only true if the debugger is started.
5844d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(Thread::Current(), *deoptimization_lock_);
5854d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    for (DeoptimizationRequest& req : deoptimization_requests_) {
5864d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      req.VisitRoots(callback, arg);
5874d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
5883b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
5893b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
5903b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
591d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StopJdwp() {
5920376e6b62bfb43b515b791f0a7afed2d0d8030bcSebastien Hertz  // Prevent the JDWP thread from processing JDWP incoming packets after we close the connection.
5930376e6b62bfb43b515b791f0a7afed2d0d8030bcSebastien Hertz  Disposed();
594376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  delete gJdwpState;
595719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  gJdwpState = nullptr;
596475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  delete gRegistry;
597719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  gRegistry = nullptr;
598719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  delete alloc_tracker_lock_;
599719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  alloc_tracker_lock_ = nullptr;
6004d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  delete deoptimization_lock_;
6014d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  deoptimization_lock_ = nullptr;
602872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
603872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
604767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesvoid Dbg::GcDidFinish() {
605767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
60600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
6074d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Sending heap info to DDM";
6087162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(gDdmHpifWhen);
609767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
610767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
61100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
6124d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Dumping heap to DDM";
6136a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(false);
614767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
615767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
61600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
6174d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Dumping native heap to DDM";
6186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(true);
619767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
620767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
621767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
6224ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesvoid Dbg::SetJdwpAllowed(bool allowed) {
6234ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes  gJdwpAllowed = allowed;
6244ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes}
6254ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
626872d4ec7225444d9400d30f9027247deb91012fdElliott HughesDebugInvokeReq* Dbg::GetInvokeReq() {
627475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return Thread::Current()->GetInvokeReq();
628475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
629475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
630475fc23a4a7f35d1be87ea0b06c80df317a720acElliott HughesThread* Dbg::GetDebugThread() {
631475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
632475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
633475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
634475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::ClearWaitForEventThread() {
635475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gJdwpState->ClearWaitForEventThread();
636872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
637872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
638872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Connected() {
6393bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(!gDebuggerConnected);
6404dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "JDWP has attached";
6413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gDebuggerConnected = true;
6428696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = false;
6438696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
6448696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
6458696433d1b3d8ba15288483b777edd888de69135Elliott Hughesvoid Dbg::Disposed() {
6468696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = true;
6478696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
6488696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
6498696433d1b3d8ba15288483b777edd888de69135Elliott Hughesbool Dbg::IsDisposed() {
6508696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return gDisposed;
651872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
652872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
653a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::GoActive() {
654a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Enable all debugging features, including scans for breakpoints.
655a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // This is a no-op if we're already active.
656a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Only called from the JDWP handler thread.
657a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (gDebuggerActive) {
658a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return;
659a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
660a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
661c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  {
662c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected?
66309bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
664c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    CHECK_EQ(gBreakpoints.size(), 0U);
665c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  }
666a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
667138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  {
6684d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(Thread::Current(), *deoptimization_lock_);
6694d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    CHECK_EQ(deoptimization_requests_.size(), 0U);
6704d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    CHECK_EQ(full_deoptimization_event_count_, 0U);
6717ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    CHECK_EQ(delayed_full_undeoptimization_count_, 0U);
67242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(dex_pc_change_event_ref_count_, 0U);
67342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(method_enter_event_ref_count_, 0U);
67442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(method_exit_event_ref_count_, 0U);
67542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(field_read_event_ref_count_, 0U);
67642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(field_write_event_ref_count_, 0U);
67742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    CHECK_EQ(exception_catch_event_ref_count_, 0U);
678138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
679138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
68062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
68162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
68262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
68362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
68462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_NE(old_state, kRunnable);
685138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetInstrumentation()->EnableDeoptimization();
68642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz  instrumentation_events_ = 0;
687a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  gDebuggerActive = true;
68862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
68962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
69062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
69162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  LOG(INFO) << "Debugger is active";
692872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
693872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
694872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Disconnected() {
695234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  CHECK(gDebuggerConnected);
696234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
697c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  LOG(INFO) << "Debugger is no longer active";
698234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
69962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread
70062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener
70162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // and clear the object registry.
70262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
70362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
70462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
70562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
706aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz
707aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  // Debugger may not be active at this point.
708aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  if (gDebuggerActive) {
709aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    {
710aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // Since we're going to disable deoptimization, we clear the deoptimization requests queue.
711aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // This prevents us from having any pending deoptimization request when the debugger attaches
712aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // to us again while no event has been requested yet.
7134d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      MutexLock mu(Thread::Current(), *deoptimization_lock_);
7144d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.clear();
7154d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      full_deoptimization_event_count_ = 0U;
7167ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      delayed_full_undeoptimization_count_ = 0U;
717aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    }
71842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    if (instrumentation_events_ != 0) {
71942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener,
72042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz                                                    instrumentation_events_);
72142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      instrumentation_events_ = 0;
72242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    }
723aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    runtime->GetInstrumentation()->DisableDeoptimization();
724aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    gDebuggerActive = false;
725aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  }
726234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gRegistry->Clear();
727234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gDebuggerConnected = false;
72862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
72962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
730872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
731872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
732c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsDebuggerActive() {
7333bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gDebuggerActive;
734872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
735872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
736c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsJdwpConfigured() {
7373bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gJdwpConfigured;
738872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
739872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
740872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesint64_t Dbg::LastDebuggerActivity() {
741ca9515205010099d006ac2fac244348a1e673dcbElliott Hughes  return gJdwpState->LastDebuggerActivity();
742872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
743872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
744872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::UndoDebuggerSuspensions() {
745234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
746872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
747872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
74888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetClassName(JDWP::RefTypeId class_id) {
7492dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id);
7503d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (o == NULL) {
751436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return "NULL";
7527b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
75364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
75488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id));
7553d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
7563d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (!o->IsClass()) {
7577934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    return StringPrintf("non-class %p", o);  // This is only used for debugging output anyway.
7583d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
759f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  return DescriptorToName(o->AsClass()->GetDescriptor().c_str());
7603d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes}
7613d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
76288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) {
763436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
7642dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
765436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
766436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
7672435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
76888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  class_object_id = gRegistry->Add(c);
769436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
7708696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
7718696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
77288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) {
7733d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
7742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
7753d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c == NULL) {
7763d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
7773d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
7783d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c->IsInterface()) {
7793d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    // http://code.google.com/p/android/issues/detail?id=20856
78088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = 0;
7813d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else {
78288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = gRegistry->Add(c->GetSuperClass());
7833d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
7843d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
785872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
786872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
787436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
7882dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
78964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
790436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
791436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
792436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader()));
793436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
794872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
795872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
796436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
797436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
7982dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
799436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
800436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
8017b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
802436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
803436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask;
804436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
805de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set,
806de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  // not interfaces.
807436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
808de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  if ((access_flags & kAccInterface) == 0) {
809de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban    access_flags |= kAccSuper;
810de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  }
811436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
812436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAdd4BE(pReply, access_flags);
813436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
814436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
815872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
816872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
817f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott HughesJDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply)
818f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
8192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
82064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
821f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
822f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
823f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
824f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  // Ensure all threads are suspended while we read objects' lock words.
825f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Thread* self = Thread::Current();
8265426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  CHECK_EQ(self->GetState(), kRunnable);
8275426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  self->TransitionFromRunnableToSuspended(kSuspended);
8285426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  Runtime::Current()->GetThreadList()->SuspendAll();
829f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
830f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  MonitorInfo monitor_info(o);
831f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
8325426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  Runtime::Current()->GetThreadList()->ResumeAll();
8335426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  self->TransitionFromSuspendedToRunnable();
834f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
835d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (monitor_info.owner_ != NULL) {
836d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer()));
837f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else {
838f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    expandBufAddObjectId(reply, gRegistry->Add(NULL));
839f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
840d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.entry_count_);
841d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.waiters_.size());
842d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) {
843d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer()));
844f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
845f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  return JDWP::ERR_NONE;
846f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes}
847f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
848734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott HughesJDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id,
849734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes                                      std::vector<JDWP::ObjectId>& monitors,
85052d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                      std::vector<uint32_t>& stack_depths) {
8514993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
8524993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
8534993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  Thread* thread;
8544993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
8554993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  if (error != JDWP::ERR_NONE) {
8564993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return error;
8574993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
8584993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
8594993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
8604993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
8614993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8624993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  struct OwnedMonitorVisitor : public StackVisitor {
8637a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    OwnedMonitorVisitor(Thread* thread, Context* context)
8644993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
8657a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context), current_stack_depth(0) {}
8664993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8674993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
8684993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // annotalysis.
8694993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
8704993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      if (!GetMethod()->IsRuntimeMethod()) {
8714993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        Monitor::VisitLocks(this, AppendOwnedMonitors, this);
872734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes        ++current_stack_depth;
8734993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      }
8744993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      return true;
8754993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
8764993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) {
8787a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg);
879734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes      visitor->monitors.push_back(owned_monitor);
880734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes      visitor->stack_depths.push_back(visitor->current_stack_depth);
8814993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
8824993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
883734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    size_t current_stack_depth;
8842dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    std::vector<mirror::Object*> monitors;
885734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    std::vector<uint32_t> stack_depths;
8864993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  };
887700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
8887a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  OwnedMonitorVisitor visitor(thread, context.get());
8894993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  visitor.WalkStack();
8904993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8914993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  for (size_t i = 0; i < visitor.monitors.size(); ++i) {
8924993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    monitors.push_back(gRegistry->Add(visitor.monitors[i]));
893734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    stack_depths.push_back(visitor.stack_depths[i]);
8944993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
8954993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8964993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  return JDWP::ERR_NONE;
8974993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes}
8984993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
89952d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien HertzJDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id,
90052d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                         JDWP::ObjectId& contended_monitor) {
901f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
902f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
903f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
904f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
905f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
906f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
907f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
908f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
909f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
910f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
911f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
912f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  contended_monitor = gRegistry->Add(Monitor::GetContendedMonitor(thread));
913f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
914f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
915f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
916f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
917ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott HughesJDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids,
918ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes                                       std::vector<uint64_t>& counts)
919ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
920412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
921412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
9222dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
923ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  counts.clear();
924ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  for (size_t i = 0; i < class_ids.size(); ++i) {
925ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    JDWP::JdwpError status;
9262dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_ids[i], status);
927ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    if (c == NULL) {
928ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes      return status;
929ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    }
930ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    classes.push_back(c);
931ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    counts.push_back(0);
932ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  }
933412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CountInstances(classes, false, &counts[0]);
934ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  return JDWP::ERR_NONE;
935ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes}
936ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes
9373b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott HughesJDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, std::vector<JDWP::ObjectId>& instances)
9383b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
939412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
940412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  // We only want reachable instances, so do a GC.
941412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
9423b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  JDWP::JdwpError status;
9432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
944412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (c == nullptr) {
9453b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    return status;
9463b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
9472dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
9483b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances);
9493b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
9503b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    instances.push_back(gRegistry->Add(raw_instances[i]));
9513b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
9523b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  return JDWP::ERR_NONE;
9533b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes}
9543b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes
9550cbaff584244ee767027aff35cd3c625aaee2994Elliott HughesJDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count,
9560cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes                                         std::vector<JDWP::ObjectId>& referring_objects)
9570cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
958412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
959412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
9602dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
96164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
9620cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
9630cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
9642dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
965412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->GetReferringObjects(o, max_count, raw_instances);
9660cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
9670cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    referring_objects.push_back(gRegistry->Add(raw_instances[i]));
9680cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
9690cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  return JDWP::ERR_NONE;
9700cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes}
9710cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes
97264f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id)
97364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
974e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
975e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
976e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
977e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
97864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisableCollection(object_id);
97964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
98064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
98164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
98264f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id)
98364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
984e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
985e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI
986e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // also ignores these cases and never return an error. However it's not obvious why this command
987e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // should behave differently from DisableCollection and IsCollected commands. So let's be more
988e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // strict and return an error if this happens.
989e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
990e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
991e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
99264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->EnableCollection(object_id);
99364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
99464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
99564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
99664f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected)
99764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
99865637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  if (object_id == 0) {
99965637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    // Null object id is invalid.
100065637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
100165637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  }
1002e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However
100365637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  // the RI seems to ignore this and assume object has been collected.
100465637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
1005e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
100665637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    is_collected = true;
100765637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  } else {
100865637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    is_collected = gRegistry->IsCollected(object_id);
1009e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
101064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
101164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
101264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
101364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count)
101464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
101564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisposeObject(object_id, reference_count);
101664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
101764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
10184d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertzstatic JDWP::JdwpTypeTag GetTypeTag(mirror::Class* klass)
10194d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
10204d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  DCHECK(klass != nullptr);
10214d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  if (klass->IsArrayClass()) {
10224d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_ARRAY;
10234d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  } else if (klass->IsInterface()) {
10244d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_INTERFACE;
10254d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  } else {
10264d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_CLASS;
10274d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  }
10284d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz}
10294d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz
103088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1031436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
10322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1033436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1034436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
10357b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1036436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
10374d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag type_tag = GetTypeTag(c);
10384d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  expandBufAdd1(pReply, type_tag);
103988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  expandBufAddRefTypeId(pReply, class_id);
1040436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1041872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1042872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
10437b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughesvoid Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) {
1044a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Get the complete list of reference classes (i.e. all classes except
1045a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // the primitive types).
1046a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Returns a newly-allocated buffer full of RefTypeId values.
1047a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  struct ClassListCreator {
1048ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) {
10497b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    }
10507b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
10512dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static bool Visit(mirror::Class* c, void* arg) {
1052a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return reinterpret_cast<ClassListCreator*>(arg)->Visit(c);
1053a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1054a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
105564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
105664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
105764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS {
1058a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (!c->IsPrimitive()) {
105964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes        classes.push_back(gRegistry->AddRefType(c));
1060a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
1061a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return true;
1062a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1063a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
10647b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    std::vector<JDWP::RefTypeId>& classes;
1065a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
1066a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
10677b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  ClassListCreator clc(classes);
1068a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc);
1069872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1070872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
107188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) {
1072436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
10732dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1074436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1075436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
10767b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
10777b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
1078a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (c->IsArrayClass()) {
1079a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
1080a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = JDWP::TT_ARRAY;
1081a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
1082a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsErroneous()) {
1083a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_ERROR;
1084a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    } else {
1085a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED;
1086a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1087a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
1088a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
1089a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
1090a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (pDescriptor != NULL) {
1091f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier    *pDescriptor = c->GetDescriptor();
1092a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
1093436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1094872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1095872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1096c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughesvoid Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) {
10972dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
10986fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes);
10996fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  ids.clear();
11006fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  for (size_t i = 0; i < classes.size(); ++i) {
11016fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes    ids.push_back(gRegistry->Add(classes[i]));
11026fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  }
1103872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1104872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
110564f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply)
110664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
11072dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
110864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
11092435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
11102435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
11112435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
11124d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass());
111364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass());
11142435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
11152435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAdd1(pReply, type_tag);
11162435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAddRefTypeId(pReply, type_id);
11172435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
11182435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1119872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1120872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1121fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan RogersJDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) {
11221fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  JDWP::JdwpError status;
11232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
11241fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  if (c == NULL) {
11251fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes    return status;
11267b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1127f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  *signature = c->GetDescriptor();
11281fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  return JDWP::ERR_NONE;
1129872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1130872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
113188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result) {
1132436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1134436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1135436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
11367b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1137b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz  if (c->IsProxyClass()) {
1138b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz    return JDWP::ERR_ABSENT_INFORMATION;
1139b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz  }
1140f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  result = c->GetSourceFile();
1141436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1142872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1143872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
114488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) {
11459837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
11462dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
114764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
1148546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
1149546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
11509837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  tag = TagFromObject(soa, o);
1151546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  return JDWP::ERR_NONE;
1152872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1153872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1154aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughessize_t Dbg::GetTagWidth(JDWP::JdwpTag tag) {
1155dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  switch (tag) {
1156dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_VOID:
1157dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 0;
1158dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BYTE:
1159dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BOOLEAN:
1160dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 1;
1161dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CHAR:
1162dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_SHORT:
1163dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 2;
1164dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_FLOAT:
1165dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_INT:
1166dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 4;
1167dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_ARRAY:
1168dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_OBJECT:
1169dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_STRING:
1170dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD:
1171dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD_GROUP:
1172dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_LOADER:
1173dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_OBJECT:
1174dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return sizeof(JDWP::ObjectId);
1175dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_DOUBLE:
1176dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_LONG:
1177dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 8;
1178dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  default:
11793d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Unknown tag " << tag;
1180dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return -1;
1181dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1182872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1183872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
118488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int& length) {
11853d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
11862dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
11873d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (a == NULL) {
11883d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
118924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
11903d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  length = a->GetLength();
11913d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1192872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1193872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
119488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) {
11953d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
11962dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
11979837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  if (a == nullptr) {
11983d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
11993d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
120024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
120124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) {
120224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
12033d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
120424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
1205f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  std::string descriptor(a->GetClass()->GetDescriptor());
120624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
120724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
12083d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd1(pReply, tag);
12093d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd4BE(pReply, count);
12103d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
121124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (IsPrimitiveTag(tag)) {
121224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    size_t width = GetTagWidth(tag);
121324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    uint8_t* dst = expandBufAddSpace(pReply, count * width);
121424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    if (width == 8) {
1215ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t), 0));
121624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]);
121724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 4) {
1218ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t), 0));
121924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]);
122024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 2) {
1221ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t), 0));
122224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]);
122324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else {
1224ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t), 0));
122524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      memcpy(dst, &src[offset * width], count * width);
122624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
122724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  } else {
12289837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
12292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>();
123024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    for (int i = 0; i < count; ++i) {
12312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* element = oa->Get(offset + i);
12329837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element)
12339837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers                                                        : tag;
123424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAdd1(pReply, specific_tag);
123524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAddObjectId(pReply, gRegistry->Add(element));
123624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
123724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
123824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
12393d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1240872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1241872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1242ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogerstemplate <typename T>
1243ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstatic void CopyArrayData(mirror::Array* a, JDWP::Request& src, int offset, int count)
1244ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    NO_THREAD_SAFETY_ANALYSIS {
1245ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  // TODO: fix when annotalysis correctly handles non-member functions.
12464b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  DCHECK(a->GetClass()->IsPrimitiveArray());
12474b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1248ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset));
12494b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  for (int i = 0; i < count; ++i) {
12504b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    *dst++ = src.ReadValue(sizeof(T));
12514b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  }
12524b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
12534b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
125488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count,
12554b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                      JDWP::Request& request)
1256b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
12573d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
12584b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  mirror::Array* dst = DecodeArray(array_id, status);
12594b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (dst == NULL) {
12603d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
12613d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
1262f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
12634b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) {
1264f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
12653d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
1266f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1267f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  std::string descriptor = dst->GetClass()->GetDescriptor();
1268f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
1269f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
1270f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  if (IsPrimitiveTag(tag)) {
1271f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    size_t width = GetTagWidth(tag);
1272f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    if (width == 8) {
12734b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint64_t>(dst, request, offset, count);
1274f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 4) {
12754b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint32_t>(dst, request, offset, count);
1276f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 2) {
12774b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint16_t>(dst, request, offset, count);
1278f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else {
12794b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint8_t>(dst, request, offset, count);
1280f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1281f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  } else {
12824b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>();
1283f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    for (int i = 0; i < count; ++i) {
12844b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      JDWP::ObjectId id = request.ReadObjectId();
12852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
128664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes      if (o == ObjectRegistry::kInvalidObject) {
1287436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
1288436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes      }
1289d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      oa->Set<false>(offset + i, o);
1290f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1291f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1292f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
12933d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1294872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1295872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
12967b3cdfcca472b779cf8745fb8460935e56229f11Elliott HughesJDWP::ObjectId Dbg::CreateString(const std::string& str) {
12972dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  return gRegistry->Add(mirror::String::AllocFromModifiedUtf8(Thread::Current(), str.c_str()));
1298872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1299872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
130088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) {
1301436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
13022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1303436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1304436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
13057b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
130650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  new_object = gRegistry->Add(c->AllocObject(Thread::Current()));
1307436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1308872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1309872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1310bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes/*
1311bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".
1312bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes */
131388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length,
131400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                       JDWP::ObjectId& new_array) {
1315436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
13162dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(array_class_id, status);
1317436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1318436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
13197b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
13206fac447555dc94a935b78198479cce645c837b89Ian Rogers  new_array = gRegistry->Add(mirror::Array::Alloc<true>(Thread::Current(), c, length,
13216fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                        c->GetComponentSize(),
13226fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                        Runtime::Current()->GetHeap()->GetCurrentAllocator()));
1323436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1324872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1325872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
132688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesbool Dbg::MatchType(JDWP::RefTypeId instance_class_id, JDWP::RefTypeId class_id) {
1327436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
13282dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c1 = DecodeClass(instance_class_id, status);
1329a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  CHECK(c1 != NULL);
13302dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c2 = DecodeClass(class_id, status);
1331a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  CHECK(c2 != NULL);
1332123756a041baf8421ed933312605daa5ef082f6fSebastien Hertz  return c2->IsAssignableFrom(c1);
1333872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1334872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1335ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::FieldId ToFieldId(const mirror::ArtField* f)
1336b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1337590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
133803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f));
133903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
134003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1341ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::MethodId ToMethodId(const mirror::ArtMethod* m)
1342b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1343590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
134403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m));
134503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
134603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1347ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtField* FromFieldId(JDWP::FieldId fid)
1348b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1349590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
1350ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtField*>(static_cast<uintptr_t>(fid));
1351aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes}
1352aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1353ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtMethod* FromMethodId(JDWP::MethodId mid)
1354b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1355590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
1356ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtMethod*>(static_cast<uintptr_t>(mid));
135703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
135803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1359ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic void SetLocation(JDWP::JdwpLocation& location, mirror::ArtMethod* m, uint32_t dex_pc)
1360b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
136191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (m == NULL) {
136291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    memset(&location, 0, sizeof(location));
136391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  } else {
13642dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = m->GetDeclaringClass();
13654d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    location.type_tag = GetTypeTag(c);
1366cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    location.class_id = gRegistry->AddRefType(c);
1367748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    location.method_id = ToMethodId(m);
1368cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    location.dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint64_t>(-1) : dex_pc;
136991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
1370d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes}
1371d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
1372a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetMethodName(JDWP::MethodId method_id)
1373b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1374ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
13756d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  return MethodHelper(m).GetName();
1376872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1377872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1378a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetFieldName(JDWP::FieldId field_id)
1379a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1380ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
1381a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes  return FieldHelper(f).GetName();
1382a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes}
1383a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes
1384a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes/*
1385a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * Augment the access flags for synthetic methods and fields by setting
1386a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * the (as described by the spec) "0xf0000000 bit".  Also, strip out any
1387a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * flags not specified by the Java programming language.
1388a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes */
1389a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughesstatic uint32_t MangleAccessFlags(uint32_t accessFlags) {
1390a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  accessFlags &= kAccJavaFlagsMask;
1391a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  if ((accessFlags & kAccSynthetic) != 0) {
1392a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    accessFlags |= 0xf0000000;
1393a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1394a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return accessFlags;
1395872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1396872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1397dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes/*
1398b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come first. Debuggers
1399b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * expect slots to begin with arguments, but dex code places them at
1400b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * the end.
1401dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes */
1402b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Haostatic uint16_t MangleSlot(uint16_t slot, mirror::ArtMethod* m)
1403b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1404b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
1405cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1406cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // We should not get here for a method without code (native, proxy or abstract). Log it and
1407cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // return the slot as is since all registers are arguments.
1408cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    LOG(WARNING) << "Trying to mangle slot for method without code " << PrettyMethod(m);
1409cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return slot;
1410cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1411b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1412b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1413b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot >= locals_size) {
1414b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - locals_size;
1415b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1416b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + ins_size;
1417dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1418dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1419dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1420b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao/*
1421b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come last. Reverts
1422b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * slots to dex style argument placement.
1423b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao */
1424ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic uint16_t DemangleSlot(uint16_t slot, mirror::ArtMethod* m)
1425b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1426b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
1427cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1428cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // We should not get here for a method without code (native, proxy or abstract). Log it and
1429cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // return the slot as is since all registers are arguments.
1430cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    LOG(WARNING) << "Trying to demangle slot for method without code " << PrettyMethod(m);
1431cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return slot;
1432cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1433b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1434b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1435b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot < ins_size) {
1436b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + locals_size;
1437b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1438b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - ins_size;
1439dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1440dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1441dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
144288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1443436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1445436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1446436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
14477b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1448a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1449a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t instance_field_count = c->NumInstanceFields();
1450a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t static_field_count = c->NumStaticFields();
1451a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1452a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, instance_field_count + static_field_count);
1453a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1454a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < instance_field_count + static_field_count; ++i) {
1455ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count);
14566d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    FieldHelper fh(f);
1457a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddFieldId(pReply, ToFieldId(f));
14586d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, fh.GetName());
14596d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, fh.GetTypeDescriptor());
1460c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1461a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1462a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1463a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1464a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags()));
1465a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1466436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1467a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1468a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
146988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic,
147000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                           JDWP::ExpandBuf* pReply) {
1471436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1473436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1474436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
14757b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1476a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1477a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t direct_method_count = c->NumDirectMethods();
1478a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t virtual_method_count = c->NumVirtualMethods();
1479a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1480a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, direct_method_count + virtual_method_count);
1481a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1482a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) {
1483ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
14846d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    MethodHelper mh(m);
1485a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddMethodId(pReply, ToMethodId(m));
14866d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, mh.GetName());
1487d91d6d6a80748f277fd938a412211e5af28913b1Ian Rogers    expandBufAddUtf8String(pReply, mh.GetSignature().ToString());
1488c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1489a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1490a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1491a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1492a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags()));
1493a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1494436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1495a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1496a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
149788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1498436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
1499f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  Thread* self = Thread::Current();
1500f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  StackHandleScope<1> hs(self);
1501f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  Handle<mirror::Class> c(hs.NewHandle(DecodeClass(class_id, status)));
1502f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  if (c.Get() == nullptr) {
1503436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
15047b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1505f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  size_t interface_count = c->NumDirectInterfaces();
1506a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, interface_count);
1507a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < interface_count; ++i) {
1508f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier    expandBufAddRefTypeId(pReply,
1509f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier                          gRegistry->AddRefType(mirror::Class::GetDirectInterface(self, c, i)));
1510a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1511436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1512872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1513872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
151488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply)
1515b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
151603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  struct DebugCallbackContext {
151703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    int numItems;
151803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    JDWP::ExpandBuf* pReply;
151903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
15202435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    static bool Callback(void* context, uint32_t address, uint32_t line_number) {
152103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
152203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      expandBufAdd8BE(pContext->pReply, address);
15232435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      expandBufAdd4BE(pContext->pReply, line_number);
152403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      pContext->numItems++;
1525f2910eef247b45ce1d489e323b36b5de6b6157aaSebastien Hertz      return false;
152603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
152703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
1528ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
15296d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  MethodHelper mh(m);
1530cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  const DexFile::CodeItem* code_item = mh.GetCodeItem();
153103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  uint64_t start, end;
1532cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1533cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    DCHECK(m->IsNative() || m->IsProxyMethod());
153403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = -1;
153503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    end = -1;
153603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  } else {
153703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = 0;
153814f0db92225d34622fa5cb1a6dc9287334aaf6c7jeffhao    // Return the index of the last instruction
1539cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    end = code_item->insns_size_in_code_units_ - 1;
154003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  }
154103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
154203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, start);
154303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, end);
154403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
154503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  // Add numLines later
154603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  size_t numLinesOffset = expandBufGetLength(pReply);
154703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd4BE(pReply, 0);
154803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
154903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  DebugCallbackContext context;
155003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.numItems = 0;
155103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.pReply = pReply;
155203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1553cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item != nullptr) {
1554cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
1555cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                    DebugCallbackContext::Callback, NULL, &context);
1556cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
155703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
155803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems);
1559872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1560872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
156188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1562dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  struct DebugCallbackContext {
1563b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    mirror::ArtMethod* method;
1564dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    JDWP::ExpandBuf* pReply;
1565c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    size_t variable_count;
1566c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    bool with_generic;
1567dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1568b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, const char* name, const char* descriptor, const char* signature)
1569b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1570dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1571dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1572b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao      VLOG(jdwp) << StringPrintf("    %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d", pContext->variable_count, startAddress, endAddress - startAddress, name, descriptor, signature, slot, MangleSlot(slot, pContext->method));
1573dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1574b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao      slot = MangleSlot(slot, pContext->method);
157568fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
1576dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd8BE(pContext->pReply, startAddress);
1577dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, name);
1578dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, descriptor);
1579c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      if (pContext->with_generic) {
1580dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes        expandBufAddUtf8String(pContext->pReply, signature);
1581dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      }
1582dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, endAddress - startAddress);
1583dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, slot);
1584dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1585c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      ++pContext->variable_count;
1586dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    }
1587dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  };
1588ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
15896d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  MethodHelper mh(m);
1590dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1591c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // arg_count considers doubles and longs to take 2 units.
1592c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // variable_count considers everything to take 1 unit.
15936d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  std::string shorty(mh.GetShorty());
1594ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  expandBufAdd4BE(pReply, mirror::ArtMethod::NumArgRegisters(shorty));
1595dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1596c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // We don't know the total number of variables yet, so leave a blank and update it later.
1597c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  size_t variable_count_offset = expandBufGetLength(pReply);
1598dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  expandBufAdd4BE(pReply, 0);
1599dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1600dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  DebugCallbackContext context;
1601b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  context.method = m;
1602dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  context.pReply = pReply;
1603c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.variable_count = 0;
1604c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.with_generic = with_generic;
1605dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1606cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  const DexFile::CodeItem* code_item = mh.GetCodeItem();
1607cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item != nullptr) {
1608cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL,
1609cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                    DebugCallbackContext::Callback, &context);
1610cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1611dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1612c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count);
1613872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1614872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1615579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value,
1616579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                                  JDWP::ExpandBuf* pReply) {
1617579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  mirror::ArtMethod* m = FromMethodId(method_id);
1618579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  JDWP::JdwpTag tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty());
1619579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  OutputJValue(tag, return_value, pReply);
1620579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1621579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
16223f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value,
16233f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                           JDWP::ExpandBuf* pReply) {
16243f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  mirror::ArtField* f = FromFieldId(field_id);
16253f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
16263f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  OutputJValue(tag, field_value, pReply);
16273f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
16283f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
16299777ba230c83a0edcbda2cf7b208339e77bf171bElliott HughesJDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id,
16309777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes                                  std::vector<uint8_t>& bytecodes)
16319777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1632ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
16339777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  if (m == NULL) {
16349777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    return JDWP::ERR_INVALID_METHODID;
16359777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
16369777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  MethodHelper mh(m);
16379777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const DexFile::CodeItem* code_item = mh.GetCodeItem();
16389777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  size_t byte_count = code_item->insns_size_in_code_units_ * 2;
16399777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_);
16409777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* end = begin + byte_count;
16419777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  for (const uint8_t* p = begin; p != end; ++p) {
16429777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    bytecodes.push_back(*p);
16439777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
16449777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  return JDWP::ERR_NONE;
16459777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes}
16469777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes
164788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) {
164888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
1649872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1650872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
165188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) {
165288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
1653872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1654872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
165588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id,
165688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                         JDWP::FieldId field_id, JDWP::ExpandBuf* pReply,
165700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         bool is_static)
1658b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16590cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  JDWP::JdwpError status;
16602dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(ref_type_id, status);
166188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  if (ref_type_id != 0 && c == NULL) {
16620cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    return status;
16630cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
16640cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
16652dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
166664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
16673f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
16683f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1669ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
16700cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
16712dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* receiver_class = c;
16720cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class == NULL && o != NULL) {
16730cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    receiver_class = o->GetClass();
16740cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
16750cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we give up now if receiver_class is NULL?
16760cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) {
16770cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class);
16783f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_FIELDID;
16793f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1680aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
16810cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
16820cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
16830cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
16840cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
16850cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
16860cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
16870cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
16880cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
16890cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
16900cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
16910cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
16920dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
16930dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
16940dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
16950cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
16966d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
1697579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  JValue field_value;
1698579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (tag == JDWP::JT_VOID) {
1699579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    LOG(FATAL) << "Unknown tag: " << tag;
1700579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (!IsPrimitiveTag(tag)) {
1701579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetL(f->GetObject(o));
1702579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1703579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetJ(f->Get64(o));
1704aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
1705579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetI(f->Get32(o));
1706aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
1707579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  Dbg::OutputJValue(tag, &field_value, pReply);
1708579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
17093f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  return JDWP::ERR_NONE;
1710872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1711872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
171288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id,
171300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ExpandBuf* pReply) {
171488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(0, object_id, field_id, pReply, false);
17153f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
17163f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
171788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, JDWP::ExpandBuf* pReply) {
171888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true);
17193f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
17203f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
172188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id,
172200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         uint64_t value, int width, bool is_static)
1723b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
17242dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
172564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
17263f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
17273f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1728ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
17290cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
17300cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
17310cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
17320cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
17330cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
17340cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
17350cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
17360cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
17370cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
17380cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
17390cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
17403f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
17410dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
17420dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
17430dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
1744aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
17456d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
1746aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1747aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  if (IsPrimitiveTag(tag)) {
1748aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
17491bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_EQ(width, 8);
1750d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      // Debugging can't use transactional mode (runtime only).
1751d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      f->Set64<false>(o, value);
1752aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    } else {
17531bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_LE(width, 4);
1754d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      // Debugging can't use transactional mode (runtime only).
1755d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      f->Set32<false>(o, value);
1756aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    }
1757aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
17582dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* v = gRegistry->Get<mirror::Object*>(value);
175964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (v == ObjectRegistry::kInvalidObject) {
17603d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes      return JDWP::ERR_INVALID_OBJECT;
17613d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    }
17623f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    if (v != NULL) {
17632dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* field_type = FieldHelper(f).GetType();
17643f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      if (!field_type->IsAssignableFrom(v->GetClass())) {
17653f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
17663f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      }
17673f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
1768d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    // Debugging can't use transactional mode (runtime only).
1769d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    f->SetObject<false>(o, v);
1770aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
17713d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
17723d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1773872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1774872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
177588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value,
177600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   int width) {
177788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(object_id, field_id, value, width, false);
1778872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1779872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
178088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) {
178188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(0, field_id, value, width, true);
1782872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1783872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
178488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::StringToUtf8(JDWP::ObjectId string_id) {
17852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = gRegistry->Get<mirror::String*>(string_id);
178668fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  return s->ToModifiedUtf8();
1787872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1788872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1789579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) {
1790579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (IsPrimitiveTag(tag)) {
1791579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAdd1(pReply, tag);
1792579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) {
1793579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd1(pReply, return_value->GetI());
1794579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) {
1795579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd2BE(pReply, return_value->GetI());
1796579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) {
1797579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd4BE(pReply, return_value->GetI());
1798579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1799579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd8BE(pReply, return_value->GetJ());
1800579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else {
1801579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      CHECK_EQ(tag, JDWP::JT_VOID);
1802579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    }
1803579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else {
18049837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
1805579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    mirror::Object* value = return_value->GetL();
18069837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    expandBufAdd1(pReply, TagFromObject(soa, value));
1807579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAddObjectId(pReply, gRegistry->Add(value));
1808579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  }
1809579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1810579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
1811221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string& name) {
1812a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  ScopedObjectAccessUnchecked soa(Thread::Current());
1813a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1814221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1815221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1816221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) {
1817221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1818a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1819221229cb523f849f165fdafbf9785010963715daElliott Hughes
1820221229cb523f849f165fdafbf9785010963715daElliott Hughes  // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName.
18212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
1822ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* java_lang_Thread_name_field =
18232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      soa.DecodeField(WellKnownClasses::java_lang_Thread_name);
18242dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s =
18252dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object));
1826221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (s != NULL) {
1827221229cb523f849f165fdafbf9785010963715daElliott Hughes    name = s->ToModifiedUtf8();
1828221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1829221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1830872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1831872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1832221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
183300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
18342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
183564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_object == ObjectRegistry::kInvalidObject) {
18362435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
18372435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
18389837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroup");
18392435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Okay, so it's an object, but is it actually a thread?
184050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1841221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1842221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1843221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1844221229cb523f849f165fdafbf9785010963715daElliott Hughes    // Zombie threads are in the null group.
1845221229cb523f849f165fdafbf9785010963715daElliott Hughes    expandBufAddObjectId(pReply, JDWP::ObjectId(0));
184652d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    error = JDWP::ERR_NONE;
184752d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz  } else if (error == JDWP::ERR_NONE) {
184852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
184952d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    CHECK(c != nullptr);
185052d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::ArtField* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;");
185152d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    CHECK(f != NULL);
185252d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::Object* group = f->GetObject(thread_object);
185352d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    CHECK(group != NULL);
185452d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    JDWP::ObjectId thread_group_id = gRegistry->Add(group);
185552d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    expandBufAddObjectId(pReply, thread_group_id);
18562435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
18579837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
185852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz  return error;
1859872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1860872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
186188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id) {
186200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
18632dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
18649837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(thread_group != nullptr);
18659837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroupName");
18669837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
18679837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(c != nullptr);
1868ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("name", "Ljava/lang/String;");
1869499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(f != NULL);
18702dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group));
18719837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
1872499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  return s->ToModifiedUtf8();
1873872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1874872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
187588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id) {
18769837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
18772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
18789837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(thread_group != nullptr);
18799837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroupParent");
18809837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
18819837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(c != nullptr);
1882ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;");
18834e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  CHECK(f != NULL);
18842dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* parent = f->GetObject(thread_group);
18859837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
18864e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  return gRegistry->Add(parent);
1887872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1888872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1889872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetSystemThreadGroupId() {
189000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
1891ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup);
18922dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
1893365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
1894872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1895872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1896872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetMainThreadGroupId() {
189700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1898ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup);
18992dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
1900365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
1901872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1902872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1903920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff HaoJDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) {
1904920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  switch (state) {
1905920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kBlocked:
1906920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_MONITOR;
1907920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kNative:
1908920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kRunnable:
1909920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSuspended:
1910920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_RUNNING;
1911920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSleeping:
1912920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_SLEEPING;
1913920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kStarting:
1914920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTerminated:
1915920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_ZOMBIE;
1916920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTimedWaiting:
1917920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSend:
1918920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSuspension:
1919920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerToAttach:
1920138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    case kWaitingForDeoptimization:
1921920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForGcToComplete:
1922920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForCheckPointsToRun:
1923920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForJniOnLoad:
1924920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForSignalCatcherOutput:
1925920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainDebuggerLoop:
1926920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainSignalCatcherLoop:
1927920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingPerformingGc:
1928920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaiting:
1929920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_WAIT;
1930920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      // Don't add a 'default' here so the compiler can spot incompatible enum changes.
1931920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  }
1932920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  LOG(FATAL) << "Unknown thread state: " << state;
1933920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  return JDWP::TS_ZOMBIE;
1934920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao}
1935920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao
193652d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien HertzJDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus,
193752d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                     JDWP::JdwpSuspendStatus* pSuspendStatus) {
193800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1939499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
19409e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED;
19419e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
194250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1943221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1944221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1945221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1946221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1947221229cb523f849f165fdafbf9785010963715daElliott Hughes      *pThreadStatus = JDWP::TS_ZOMBIE;
1948221229cb523f849f165fdafbf9785010963715daElliott Hughes      return JDWP::ERR_NONE;
1949221229cb523f849f165fdafbf9785010963715daElliott Hughes    }
1950221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1951499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  }
1952499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
19539e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  if (IsSuspendedForDebugger(soa, thread)) {
19549e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED;
19559e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  }
195600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
1957920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  *pThreadStatus = ToJdwpThreadStatus(thread->GetState());
1958221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1959872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1960872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1961221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
196200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
196350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1964221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1965221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1966221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1967221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
19682435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
196950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
197000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  expandBufAdd4BE(pReply, thread->GetDebugSuspendCount());
19712435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1972872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1973872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1974f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott HughesJDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) {
1975f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccess soa(Thread::Current());
1976f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1977f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
1978f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1979f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
1980f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
1981f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
1982dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  thread->Interrupt(soa.Self());
1983f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
1984f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
1985f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
1986caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughesvoid Dbg::GetThreads(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& thread_ids) {
1987365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  class ThreadListVisitor {
1988365c10235438607541fa2259a5fec48061b90bd8Ian Rogers   public:
19892dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    ThreadListVisitor(const ScopedObjectAccessUnchecked& soa, mirror::Object* desired_thread_group,
199000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                      std::vector<JDWP::ObjectId>& thread_ids)
1991b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
19920dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        : soa_(soa), desired_thread_group_(desired_thread_group), thread_ids_(thread_ids) {}
1993365c10235438607541fa2259a5fec48061b90bd8Ian Rogers
1994a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    static void Visit(Thread* t, void* arg) {
1995a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t);
1996a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1997a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
199800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
199900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
200000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void Visit(Thread* t) NO_THREAD_SAFETY_ANALYSIS {
2001a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (t == Dbg::GetDebugThread()) {
2002a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and
2003a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        // query all threads, so it's easier if we just don't tell them about this thread.
2004a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
2005a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
20062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* peer = t->GetPeer();
20070dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      if (IsInDesiredThreadGroup(peer)) {
2008120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers        thread_ids_.push_back(gRegistry->Add(peer));
2009a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
2010a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
2011a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
2012365c10235438607541fa2259a5fec48061b90bd8Ian Rogers   private:
20132dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    bool IsInDesiredThreadGroup(mirror::Object* peer)
20140dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20150dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      // peer might be NULL if the thread is still starting up.
20160dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      if (peer == NULL) {
20170dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // We can't tell the debugger about this thread yet.
20180dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // TODO: if we identified threads to the debugger by their Thread*
20192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        // rather than their peer's mirror::Object*, we could fix this.
20200dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // Doing so might help us report ZOMBIE threads too.
20210dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        return false;
20220dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      }
2023c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      // Do we want threads from all thread groups?
2024c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      if (desired_thread_group_ == NULL) {
2025c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao        return true;
2026c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      }
20272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* group = soa_.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(peer);
20280dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      return (group == desired_thread_group_);
20290dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    }
20300dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao
2031dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier    const ScopedObjectAccessUnchecked& soa_;
20322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* const desired_thread_group_;
2033caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    std::vector<JDWP::ObjectId>& thread_ids_;
2034a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
2035a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
203600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
20372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
203800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ThreadListVisitor tlv(soa, thread_group, thread_ids);
203950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2040f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughes  Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv);
2041a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes}
2042a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
2043caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughesvoid Dbg::GetChildThreadGroups(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& child_thread_group_ids) {
204400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
20452dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
2046caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes
2047caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Get the ArrayList<ThreadGroup> "groups" out of this thread group...
2048ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* groups_field = thread_group->GetClass()->FindInstanceField("groups", "Ljava/util/List;");
20492dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* groups_array_list = groups_field->GetObject(thread_group);
2050872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2051caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Get the array and size out of the ArrayList<ThreadGroup>...
2052ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* array_field = groups_array_list->GetClass()->FindInstanceField("array", "[Ljava/lang/Object;");
2053ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* size_field = groups_array_list->GetClass()->FindInstanceField("size", "I");
20542dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::ObjectArray<mirror::Object>* groups_array =
20552dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>();
2056caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  const int32_t size = size_field->GetInt(groups_array_list);
2057caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes
2058caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Copy the first 'size' elements out of the array into the result.
2059caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  for (int32_t i = 0; i < size; ++i) {
2060caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    child_thread_group_ids.push_back(gRegistry->Add(groups_array->Get(i)));
2061caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  }
2062872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2063872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
206400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic int GetStackDepth(Thread* thread)
2065b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct CountStackDepthVisitor : public StackVisitor {
206793ba893c20532990a430741e0a97212900094e8cBrian Carlstrom    explicit CountStackDepthVisitor(Thread* thread)
20687a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth(0) {}
20690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
207064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
207164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
207264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
20730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!GetMethod()->IsRuntimeMethod()) {
2074f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes        ++depth;
2075f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes      }
2076530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
2077a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
2078a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    size_t depth;
2079a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  };
208008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
20817a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  CountStackDepthVisitor visitor(thread);
20820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2083a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return visitor.depth;
2084872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2085872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2086221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result) {
208700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
2088a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2089221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2090221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2091221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2092221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2093221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2094f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
2095f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
2096f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
2097221229cb523f849f165fdafbf9785010963715daElliott Hughes  result = GetStackDepth(thread);
2098221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
20998696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
21008696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2101306057fd278d75bf3794bd5243a3b6652c487d18Ian RogersJDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame,
2102306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers                                     size_t frame_count, JDWP::ExpandBuf* buf) {
21036e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  class GetFrameVisitor : public StackVisitor {
21046e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   public:
21057a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    GetFrameVisitor(Thread* thread, size_t start_frame, size_t frame_count, JDWP::ExpandBuf* buf)
2106b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
21077a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth_(0),
21086e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes          start_frame_(start_frame), frame_count_(frame_count), buf_(buf) {
21096e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      expandBufAdd4BE(buf_, frame_count_);
211003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
21110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
211200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
211300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
211400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
21150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetMethod()->IsRuntimeMethod()) {
21167934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        return true;  // The debugger can't do anything useful with a frame that has no Method*.
211703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
21186e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_ + frame_count_) {
2119530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes        return false;
212003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
21216e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_) {
21226e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::FrameId frame_id(GetFrameId());
21236e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::JdwpLocation location;
21246e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        SetLocation(location, GetMethod(), GetDexPc());
2125ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        VLOG(jdwp) << StringPrintf("    Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location;
21266e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAdd8BE(buf_, frame_id);
21276e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAddLocation(buf_, location);
21286e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      }
21296e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      ++depth_;
2130530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
213103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
21326e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes
21336e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   private:
21346e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    size_t depth_;
21356e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t start_frame_;
21366e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t frame_count_;
21376e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    JDWP::ExpandBuf* buf_;
213803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
213900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
214000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2141a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2142221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2143221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2144221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2145221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2146221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2147f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
2148f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
2149f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
21507a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetFrameVisitor visitor(thread, start_frame, frame_count, buf);
21510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
21526e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
2153872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2154872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2155872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetThreadSelfId() {
2156dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier  ScopedObjectAccessUnchecked soa(Thread::Current());
2157cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  return gRegistry->Add(soa.Self()->GetPeer());
2158872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2159872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2160475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::SuspendVM() {
216100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
2162872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2163872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2164872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ResumeVM() {
2165c61a267e98ed1038b74c33c7740414ced4a27d89Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
2166872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2167872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2168221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) {
216900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedLocalRef<jobject> peer(Thread::Current()->GetJniEnv(), NULL);
217000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
217100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
21722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id)));
217300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
217400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (peer.get() == NULL) {
217500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
217600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
217700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Suspend thread to build stack trace.
2178f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  bool timed_out;
2179d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  Thread* thread = ThreadList::SuspendThreadByPeer(peer.get(), request_suspension, true,
2180d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                                   &timed_out);
218100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (thread != NULL) {
218200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_NONE;
2183f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else if (timed_out) {
218400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_INTERNAL;
218500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  } else {
218600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
21874e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
2188872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2189872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2190221229cb523f849f165fdafbf9785010963715daElliott Hughesvoid Dbg::ResumeThread(JDWP::ObjectId thread_id) {
219100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
21922dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id);
2193a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  Thread* thread;
2194a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  {
2195a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2196a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    thread = Thread::FromManagedThread(soa, peer);
2197a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  }
21984e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  if (thread == NULL) {
21994e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    LOG(WARNING) << "No such thread for resume: " << peer;
22004e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    return;
22014e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
220200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool needs_resume;
220300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
220450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
220500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    needs_resume = thread->GetSuspendCount() > 0;
220600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
220700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (needs_resume) {
2208546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    Runtime::Current()->GetThreadList()->Resume(thread, true);
2209546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
2210872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2211872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2212872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::SuspendSelf() {
2213475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
2214872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2215872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
22160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct GetThisVisitor : public StackVisitor {
22177a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
2218b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
22197a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, context), this_object(NULL), frame_id(frame_id) {}
222068fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
222100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
222200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
222300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
22246e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    if (frame_id != GetFrameId()) {
22250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return true;  // continue
22260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    } else {
222762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      this_object = GetThisObject();
222862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return false;
22290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
223068fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  }
2231dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
22322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* this_object;
22336e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  JDWP::FrameId frame_id;
22340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers};
2235ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
223600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersJDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id,
223700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ObjectId* result) {
223800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
223900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* thread;
224000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
224150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2242221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2243221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
2244221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
224500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
22469e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    if (!IsSuspendedForDebugger(soa, thread)) {
224700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      return JDWP::ERR_THREAD_NOT_SUSPENDED;
224800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
22496e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  }
2250700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
22517a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor visitor(thread, context.get(), frame_id);
22520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
22536e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  *result = gRegistry->Add(visitor.this_object);
22546e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
22550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
22560399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
2257cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien HertzJDWP::JdwpError Dbg::GetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot,
2258cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                   JDWP::JdwpTag tag, uint8_t* buf, size_t width) {
22590399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct GetLocalVisitor : public StackVisitor {
22609837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    GetLocalVisitor(const ScopedObjectAccessUnchecked& soa, Thread* thread, Context* context,
22619837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers                    JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width)
2262b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
22639837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        : StackVisitor(thread, context), soa_(soa), frame_id_(frame_id), slot_(slot), tag_(tag),
2264cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz          buf_(buf), width_(width), error_(JDWP::ERR_NONE) {}
2265ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
226600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
226700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
226800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
22690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetFrameId() != frame_id_) {
22700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        return true;  // Not our frame, carry on.
22710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
22720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // TODO: check that the tag is compatible with the actual type of the slot!
2273cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      // TODO: check slot is valid for this method or return INVALID_SLOT error.
2274ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* m = GetMethod();
2275cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      if (m->IsNative()) {
2276cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        // We can't read local value from native method.
2277cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        error_ = JDWP::ERR_OPAQUE_FRAME;
2278cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        return false;
2279cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      }
22800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      uint16_t reg = DemangleSlot(slot_, m);
22810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
22820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      switch (tag_) {
22830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_BOOLEAN:
22840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
22850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
22862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
22870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get boolean local " << reg << " = " << intVal;
22880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set1(buf_+1, intVal != 0);
22890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
22900399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
22910399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_BYTE:
22920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
22930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
22942bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
22950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get byte local " << reg << " = " << intVal;
22960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set1(buf_+1, intVal);
22970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
22980399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
22990399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_SHORT:
23000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CHAR:
23010399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23020399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 2U);
23032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
23040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get short/char local " << reg << " = " << intVal;
23050399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set2BE(buf_+1, intVal);
23060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_INT:
23092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        {
23102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 4U);
23112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
23122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          VLOG(jdwp) << "get int local " << reg << " = " << intVal;
23132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          JDWP::Set4BE(buf_+1, intVal);
23142bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
23152bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        break;
23160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_FLOAT:
23170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 4U);
23192bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kFloatVReg);
23200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get int/float local " << reg << " = " << intVal;
23210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set4BE(buf_+1, intVal);
23220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_ARRAY:
23250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
23272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
23280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get array local " << reg << " = " << o;
2329590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier          if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
23300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            LOG(FATAL) << "Register " << reg << " expected to hold array: " << o;
23310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
23320399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
23330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CLASS_LOADER:
23360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CLASS_OBJECT:
23370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_OBJECT:
23380399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_STRING:
23390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_THREAD:
23400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_THREAD_GROUP:
23410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
23432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
23440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get object local " << reg << " = " << o;
2345590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier          if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
23460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            LOG(FATAL) << "Register " << reg << " expected to hold object: " << o;
23470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
23489837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers          tag_ = TagFromObject(soa_, o);
23490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
23500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_DOUBLE:
23532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        {
23542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 8U);
23552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t lo = GetVReg(m, reg, kDoubleLoVReg);
23562bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t hi = GetVReg(m, reg + 1, kDoubleHiVReg);
23572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t longVal = (hi << 32) | lo;
23582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
23592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          JDWP::Set8BE(buf_+1, longVal);
23602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
23612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        break;
23620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_LONG:
23630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23640399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 8U);
23652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t lo = GetVReg(m, reg, kLongLoVReg);
23662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t hi = GetVReg(m, reg + 1, kLongHiVReg);
23670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          uint64_t longVal = (hi << 32) | lo;
23680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
23690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set8BE(buf_+1, longVal);
23700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      default:
23730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        LOG(FATAL) << "Unknown tag " << tag_;
23740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23750399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
2376ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
23770399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // Prepend tag, which may have been updated.
23780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      JDWP::Set1(buf_, tag_);
23790399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return false;
23800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
23819837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    const ScopedObjectAccessUnchecked& soa_;
23820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::FrameId frame_id_;
23830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const int slot_;
23840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    JDWP::JdwpTag tag_;
23850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    uint8_t* const buf_;
23860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const size_t width_;
2387cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    JDWP::JdwpError error_;
23880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
238900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
239000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2391a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2392221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2393221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2394221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2395cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return error;
2396221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2397cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  // TODO check thread is suspended by the debugger ?
2398700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
23999837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  GetLocalVisitor visitor(soa, thread, context.get(), frame_id, slot, tag, buf, width);
24000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2401cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  return visitor.error_;
24020399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
24030399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
2404cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien HertzJDWP::JdwpError Dbg::SetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot,
2405cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                   JDWP::JdwpTag tag, uint64_t value, size_t width) {
24060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SetLocalVisitor : public StackVisitor {
24077a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    SetLocalVisitor(Thread* thread, Context* context,
24080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                    JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint64_t value,
2409ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers                    size_t width)
2410b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
24117a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context),
2412cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz          frame_id_(frame_id), slot_(slot), tag_(tag), value_(value), width_(width),
2413cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz          error_(JDWP::ERR_NONE) {}
2414ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
241500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
241600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
241700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
24180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetFrameId() != frame_id_) {
24190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        return true;  // Not our frame, carry on.
2420ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes      }
24210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // TODO: check that the tag is compatible with the actual type of the slot!
2422cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      // TODO: check slot is valid for this method or return INVALID_SLOT error.
2423ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* m = GetMethod();
2424cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      if (m->IsNative()) {
2425cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        // We can't read local value from native method.
2426cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        error_ = JDWP::ERR_OPAQUE_FRAME;
2427cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        return false;
2428cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      }
24290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      uint16_t reg = DemangleSlot(slot_, m);
24300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
24310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      switch (tag_) {
24320399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_BOOLEAN:
24330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_BYTE:
24340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
24352bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
24360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_SHORT:
24380399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_CHAR:
24390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 2U);
24402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
24410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_INT:
24432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 4U);
24442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
24452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          break;
24460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_FLOAT:
24470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 4U);
24482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kFloatVReg);
24490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_ARRAY:
24510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_OBJECT:
24520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_STRING:
24530399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
24540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
24552dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value_));
245664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes          if (o == ObjectRegistry::kInvalidObject) {
24570399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            UNIMPLEMENTED(FATAL) << "return an error code when given an invalid object to store";
24580399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
24592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), kReferenceVReg);
24600399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
24610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
24620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_DOUBLE:
24632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 8U);
24642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kDoubleLoVReg);
24652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kDoubleHiVReg);
24662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          break;
24670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_LONG:
24680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 8U);
24692bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kLongLoVReg);
24702bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kLongHiVReg);
24710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        default:
24730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          LOG(FATAL) << "Unknown tag " << tag_;
24740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24750399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
24760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return false;
2477cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    }
24780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
24790399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::FrameId frame_id_;
24800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const int slot_;
24810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::JdwpTag tag_;
24820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const uint64_t value_;
24830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const size_t width_;
2484cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    JDWP::JdwpError error_;
24850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
248600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
248700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2488a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2489221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2490221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2491221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2492cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return error;
2493221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2494cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  // TODO check thread is suspended by the debugger ?
2495700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  std::unique_ptr<Context> context(Context::Create());
24967a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  SetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, value, width);
24970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2498cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  return visitor.error_;
2499872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2500872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
25013f52eafe5577b8489f90dc8ed5981b3455206147Sebastien HertzJDWP::ObjectId Dbg::GetThisObjectIdForEvent(mirror::Object* this_object) {
25023f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // If 'this_object' isn't already in the registry, we know that we're not looking for it, so
25033f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // there's no point adding it to the registry and burning through ids.
25043f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // When registering an event request with an instance filter, we've been given an existing object
25053f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // id so it must already be present in the registry when the event fires.
25063f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = 0;
25073f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (this_object != nullptr && gRegistry->Contains(this_object)) {
25083f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    this_id = gRegistry->Add(this_object);
25093f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
25103f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  return this_id;
25113f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
25123f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2513ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersvoid Dbg::PostLocationEvent(mirror::ArtMethod* m, int dex_pc, mirror::Object* this_object,
2514579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                            int event_flags, const JValue* return_value) {
25153f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
25163f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
25173f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
25183f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
25193f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK_EQ(m->IsStatic(), this_object == nullptr);
252091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  JDWP::JdwpLocation location;
2521cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  SetLocation(location, m, dex_pc);
252291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
25233f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // We need 'this' for InstanceOnly filters only.
25243f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = GetThisObjectIdForEvent(this_object);
2525579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  gJdwpState->PostLocationEvent(&location, this_id, event_flags, return_value);
2526872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2527872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
25283f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostFieldAccessEvent(mirror::ArtMethod* m, int dex_pc,
25293f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                               mirror::Object* this_object, mirror::ArtField* f) {
25303f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
25313f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
25323f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
25333f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
25343f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(f != nullptr);
25353f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::JdwpLocation location;
25363f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  SetLocation(location, m, dex_pc);
25373f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25383f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::RefTypeId type_id = gRegistry->AddRefType(f->GetDeclaringClass());
25393f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::FieldId field_id = ToFieldId(f);
25403f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = gRegistry->Add(this_object);
25413f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25423f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  gJdwpState->PostFieldEvent(&location, type_id, field_id, this_id, nullptr, false);
25433f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
25443f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25453f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostFieldModificationEvent(mirror::ArtMethod* m, int dex_pc,
25463f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                     mirror::Object* this_object, mirror::ArtField* f,
25473f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                     const JValue* field_value) {
25483f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
25493f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
25503f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
25513f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
25523f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(f != nullptr);
25533f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(field_value != nullptr);
25543f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::JdwpLocation location;
25553f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  SetLocation(location, m, dex_pc);
25563f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25573f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::RefTypeId type_id = gRegistry->AddRefType(f->GetDeclaringClass());
25583f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::FieldId field_id = ToFieldId(f);
25593f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = gRegistry->Add(this_object);
25603f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25613f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  gJdwpState->PostFieldEvent(&location, type_id, field_id, this_id, field_value, true);
25623f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
25633f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25643f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostException(const ThrowLocation& throw_location,
2565ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                        mirror::ArtMethod* catch_method,
256664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes                        uint32_t catch_dex_pc, mirror::Throwable* exception_object) {
2567c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
25680ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers    return;
25690ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers  }
25704740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
257162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  JDWP::JdwpLocation jdwp_throw_location;
257262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SetLocation(jdwp_throw_location, throw_location.GetMethod(), throw_location.GetDexPc());
2573d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  JDWP::JdwpLocation catch_location;
2574caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  SetLocation(catch_location, catch_method, catch_dex_pc);
2575d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
25763f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // We need 'this' for InstanceOnly filters only.
25773f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = GetThisObjectIdForEvent(throw_location.GetThis());
257864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::ObjectId exception_id = gRegistry->Add(exception_object);
257964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId exception_class_id = gRegistry->AddRefType(exception_object->GetClass());
2580d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
258162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  gJdwpState->PostException(&jdwp_throw_location, exception_id, exception_class_id, &catch_location,
258262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                            this_id);
2583872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2584872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
25852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::PostClassPrepare(mirror::Class* c) {
2586c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
25874740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes    return;
25884740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  }
25894740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
25903d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes  // OLD-TODO - we currently always send both "verified" and "prepared" since
25914740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  // debuggers seem to like that.  There might be some advantage to honesty,
25924740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  // since the class may not yet be verified.
25934740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
25944d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag tag = GetTypeTag(c);
2595f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier  gJdwpState->PostClassPrepare(tag, gRegistry->Add(c), c->GetDescriptor(), state);
2596872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2597872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
259862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object,
25998379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz                         mirror::ArtMethod* m, uint32_t dex_pc,
26008379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz                         int event_flags, const JValue* return_value) {
260162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) {
26022aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes    return;
260391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
260491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
26058696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  if (IsBreakpoint(m, dex_pc)) {
26068696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    event_flags |= kBreakpoint;
260791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
260891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
260961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // If the debugger is single-stepping one of our threads, check to
261061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // see if we're that thread and we've reached a step point.
261161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  const SingleStepControl* single_step_control = thread->GetSingleStepControl();
261261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
261361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (single_step_control->is_active) {
261461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    CHECK(!m->IsNative());
261561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    if (single_step_control->step_depth == JDWP::SD_INTO) {
261661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step into method calls.  We break when the line number
261761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // or method pointer changes.  If we're in SS_MIN mode, we
261861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // always stop.
261961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (single_step_control->method != m) {
262061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
262161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new method";
262261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (single_step_control->step_size == JDWP::SS_MIN) {
262361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
262461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new instruction";
2625bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      } else if (single_step_control->ContainsDexPc(dex_pc)) {
262661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
262761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new line";
262861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
262961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else if (single_step_control->step_depth == JDWP::SD_OVER) {
263061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step over method calls.  We break when the line number is
263161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // different and the frame depth is <= the original frame
263261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth.  (We can't just compare on the method, because we
263361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // might get unrolled past it by an exception, and it's tricky
263461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // to identify recursion.)
263561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
263661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
263761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
263861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
263961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Popped up one or more frames, always trigger.
264061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
264161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
264261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (stack_depth == single_step_control->stack_depth) {
264361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Same depth, see if we moved.
264461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control->step_size == JDWP::SS_MIN) {
26458696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          event_flags |= kSingleStep;
26468696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          VLOG(jdwp) << "SS new instruction";
2647bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz        } else if (single_step_control->ContainsDexPc(dex_pc)) {
26482435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          event_flags |= kSingleStep;
26492435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          VLOG(jdwp) << "SS new line";
265091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes        }
265161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
265261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else {
265361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      CHECK_EQ(single_step_control->step_depth, JDWP::SD_OUT);
265461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Return from the current method.  We break when the frame
265561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth pops up.
265661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
265761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // This differs from the "method exit" break in that it stops
265861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // with the PC at the next instruction in the returned-to
265961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // function, rather than the end of the returning function.
266061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
266161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
266261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
266361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
266461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
26658696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
266691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    }
266791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
266891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
266991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // If there's something interesting going on, see if it matches one
267091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // of the debugger filters.
267191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (event_flags != 0) {
26728379b2256be5d2be4ad083a76e9f8ec403c4d405Sebastien Hertz    Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, return_value);
267391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
267491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes}
267591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
267642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertzsize_t* Dbg::GetReferenceCounterForEvent(uint32_t instrumentation_event) {
267742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz  switch (instrumentation_event) {
267842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kMethodEntered:
267942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &method_enter_event_ref_count_;
268042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kMethodExited:
268142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &method_exit_event_ref_count_;
268242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kDexPcMoved:
268342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &dex_pc_change_event_ref_count_;
268442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kFieldRead:
268542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &field_read_event_ref_count_;
268642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kFieldWritten:
268742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &field_write_event_ref_count_;
268842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case instrumentation::Instrumentation::kExceptionCaught:
268942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return &exception_catch_event_ref_count_;
269042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    default:
269142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      return nullptr;
269242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz  }
269342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz}
269442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz
26954d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz// Process request while all mutator threads are suspended.
26964d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) {
2697138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
26984d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  switch (request.kind) {
26994d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kNothing:
27004d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      LOG(WARNING) << "Ignoring empty deoptimization request.";
27014d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
270242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kRegisterForEvent:
270342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      VLOG(jdwp) << StringPrintf("Add debugger as listener for instrumentation event 0x%x",
270442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz                                 request.instrumentation_event);
270542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      instrumentation->AddListener(&gDebugInstrumentationListener, request.instrumentation_event);
270642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      instrumentation_events_ |= request.instrumentation_event;
270742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
270842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kUnregisterForEvent:
270942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      VLOG(jdwp) << StringPrintf("Remove debugger as listener for instrumentation event 0x%x",
271042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz                                 request.instrumentation_event);
271142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      instrumentation->RemoveListener(&gDebugInstrumentationListener,
271242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz                                      request.instrumentation_event);
271342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      instrumentation_events_ &= ~request.instrumentation_event;
271442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
27154d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullDeoptimization:
27167ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize the world ...";
27174d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->DeoptimizeEverything();
27187ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize the world DONE";
27194d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27204d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullUndeoptimization:
27217ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize the world ...";
27224d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->UndeoptimizeEverything();
27237ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize the world DONE";
27244d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27254d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveDeoptimization:
27267ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.method) << " ...";
27274d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->Deoptimize(request.method);
27287ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.method) << " DONE";
27294d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27304d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveUndeoptimization:
27317ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.method) << " ...";
27324d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->Undeoptimize(request.method);
27337ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.method) << " DONE";
27344d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27354d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    default:
27364d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      LOG(FATAL) << "Unsupported deoptimization request kind " << request.kind;
27374d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27384d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
27394d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz}
27404d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz
27417ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::DelayFullUndeoptimization() {
27427ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  MutexLock mu(Thread::Current(), *deoptimization_lock_);
27437ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  ++delayed_full_undeoptimization_count_;
27447ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  DCHECK_LE(delayed_full_undeoptimization_count_, full_deoptimization_event_count_);
27457ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
27467ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
27477ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::ProcessDelayedFullUndeoptimizations() {
27487ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  // TODO: avoid taking the lock twice (once here and once in ManageDeoptimization).
27497ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  {
27507ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    MutexLock mu(Thread::Current(), *deoptimization_lock_);
27517ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    while (delayed_full_undeoptimization_count_ > 0) {
27527ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      DeoptimizationRequest req;
27537ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      req.kind = DeoptimizationRequest::kFullUndeoptimization;
27547ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      req.method = nullptr;
27557ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      RequestDeoptimizationLocked(req);
27567ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      --delayed_full_undeoptimization_count_;
27577ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    }
27587ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  }
27597ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  ManageDeoptimization();
27607ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
27617ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
27624d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::RequestDeoptimization(const DeoptimizationRequest& req) {
27634d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  if (req.kind == DeoptimizationRequest::kNothing) {
27644d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    // Nothing to do.
27654d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    return;
27664d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
27674d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  MutexLock mu(Thread::Current(), *deoptimization_lock_);
27687ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  RequestDeoptimizationLocked(req);
27697ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
27707ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
27717ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) {
27724d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  switch (req.kind) {
277342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kRegisterForEvent: {
277442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      DCHECK_NE(req.instrumentation_event, 0u);
277542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      size_t* counter = GetReferenceCounterForEvent(req.instrumentation_event);
277642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x",
277742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz                                                req.instrumentation_event);
277842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      if (*counter == 0) {
27797d2ae437a87ceb2bdda098ab11f4da588c6a75f5Sebastien Hertz        VLOG(jdwp) << StringPrintf("Queue request #%zd to start listening to instrumentation event 0x%x",
278042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz                                   deoptimization_requests_.size(), req.instrumentation_event);
278142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz        deoptimization_requests_.push_back(req);
278242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      }
278342cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      *counter = *counter + 1;
278442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
278542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    }
278642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    case DeoptimizationRequest::kUnregisterForEvent: {
278742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      DCHECK_NE(req.instrumentation_event, 0u);
278842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      size_t* counter = GetReferenceCounterForEvent(req.instrumentation_event);
278942cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      CHECK(counter != nullptr) << StringPrintf("No counter for instrumentation event 0x%x",
279042cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz                                                req.instrumentation_event);
279142cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      *counter = *counter - 1;
279242cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      if (*counter == 0) {
27937d2ae437a87ceb2bdda098ab11f4da588c6a75f5Sebastien Hertz        VLOG(jdwp) << StringPrintf("Queue request #%zd to stop listening to instrumentation event 0x%x",
279442cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz                                   deoptimization_requests_.size(), req.instrumentation_event);
279542cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz        deoptimization_requests_.push_back(req);
279642cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      }
279742cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz      break;
279842cd43fa593e8f0427eb0ec158bef08814a6180bSebastien Hertz    }
27994d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullDeoptimization: {
28004d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK(req.method == nullptr);
28014d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      if (full_deoptimization_event_count_ == 0) {
28027ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz        VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
28037ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                   << " for full deoptimization";
28044d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz        deoptimization_requests_.push_back(req);
2805138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      }
28064d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      ++full_deoptimization_event_count_;
28074d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
28084d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
28094d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullUndeoptimization: {
28104d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK(req.method == nullptr);
2811e713d9338ad122d6b8c7997387d0c9fc464eea3eSebastien Hertz      DCHECK_GT(full_deoptimization_event_count_, 0U);
28124d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      --full_deoptimization_event_count_;
28134d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      if (full_deoptimization_event_count_ == 0) {
28147ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz        VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
28157ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                   << " for full undeoptimization";
28164d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz        deoptimization_requests_.push_back(req);
2817138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      }
28184d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
28194d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
28204d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveDeoptimization: {
28214d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK(req.method != nullptr);
28227ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
28237ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                 << " for deoptimization of " << PrettyMethod(req.method);
28244d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.push_back(req);
28254d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
28264d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
28274d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveUndeoptimization: {
28284d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK(req.method != nullptr);
28297ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
28307ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                 << " for undeoptimization of " << PrettyMethod(req.method);
28314d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.push_back(req);
28324d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
28334d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
28344d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    default: {
28354d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      LOG(FATAL) << "Unknown deoptimization request kind " << req.kind;
28364d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
2837138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2838138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2839138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz}
2840138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2841138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertzvoid Dbg::ManageDeoptimization() {
2842138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  Thread* const self = Thread::Current();
2843138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  {
2844138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    // Avoid suspend/resume if there is no pending request.
28454d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(self, *deoptimization_lock_);
28464d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    if (deoptimization_requests_.empty()) {
2847138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      return;
2848138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2849138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2850138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  CHECK_EQ(self->GetState(), kRunnable);
2851138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  self->TransitionFromRunnableToSuspended(kWaitingForDeoptimization);
2852138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  // We need to suspend mutator threads first.
2853138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  Runtime* const runtime = Runtime::Current();
2854138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetThreadList()->SuspendAll();
2855138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  const ThreadState old_state = self->SetStateUnsafe(kRunnable);
28564d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  {
28574d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(self, *deoptimization_lock_);
28587ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    size_t req_index = 0;
28594d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    for (const DeoptimizationRequest& request : deoptimization_requests_) {
28607ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Process deoptimization request #" << req_index++;
28614d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      ProcessDeoptimizationRequest(request);
28624d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
28634d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    deoptimization_requests_.clear();
28644d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
2865138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
2866138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetThreadList()->ResumeAll();
2867138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  self->TransitionFromSuspendedToRunnable();
2868138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz}
2869138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2870a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic bool IsMethodPossiblyInlined(Thread* self, mirror::ArtMethod* m)
2871a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2872a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  MethodHelper mh(m);
2873a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const DexFile::CodeItem* code_item = mh.GetCodeItem();
2874a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (code_item == nullptr) {
2875a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // TODO We should not be asked to watch location in a native or abstract method so the code item
2876a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // should never be null. We could just check we never encounter this case.
2877a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    return false;
2878a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  }
2879eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<2> hs(self);
2880eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::DexCache> dex_cache(hs.NewHandle(mh.GetDexCache()));
2881eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(mh.GetClassLoader()));
2882a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  verifier::MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader,
2883a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz                                    &mh.GetClassDef(), code_item, m->GetDexMethodIndex(), m,
2884a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz                                    m->GetAccessFlags(), false, true);
2885a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  // Note: we don't need to verify the method.
2886a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  return InlineMethodAnalyser::AnalyseMethodCode(&verifier, nullptr);
2887a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
2888138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2889a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic const Breakpoint* FindFirstBreakpointForMethod(mirror::ArtMethod* m)
2890a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_) {
2891a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  for (const Breakpoint& breakpoint : gBreakpoints) {
2892a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (breakpoint.method == m) {
2893a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      return &breakpoint;
2894138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2895a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  }
2896a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  return nullptr;
2897a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
2898138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2899a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Sanity checks all existing breakpoints on the same method.
2900a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic void SanityCheckExistingBreakpoints(mirror::ArtMethod* m, bool need_full_deoptimization)
2901a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_)  {
2902a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (kIsDebugBuild) {
2903a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    for (const Breakpoint& breakpoint : gBreakpoints) {
2904a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK_EQ(need_full_deoptimization, breakpoint.need_full_deoptimization);
2905a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
2906a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
2907a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // We should have deoptimized everything but not "selectively" deoptimized this method.
2908a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK(Runtime::Current()->GetInstrumentation()->AreAllMethodsDeoptimized());
2909a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK(!Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
2910a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
2911a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // We should have "selectively" deoptimized this method.
2912a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // Note: while we have not deoptimized everything for this method, we may have done it for
2913a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // another event.
2914a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK(Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
2915a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
2916138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2917a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
2918138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2919a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Installs a breakpoint at the specified location. Also indicates through the deoptimization
2920a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// request if we need to deoptimize.
2921a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzvoid Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
2922a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  Thread* const self = Thread::Current();
2923a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  mirror::ArtMethod* m = FromMethodId(location->method_id);
2924a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  DCHECK(m != nullptr) << "No method for method id " << location->method_id;
2925a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
2926a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  MutexLock mu(self, *Locks::breakpoint_lock_);
2927a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m);
2928a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  bool need_full_deoptimization;
2929a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (existing_breakpoint == nullptr) {
2930a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is no breakpoint on this method yet: we need to deoptimize. If this method may be
2931a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // inlined, we deoptimize everything; otherwise we deoptimize only this method.
2932a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    need_full_deoptimization = IsMethodPossiblyInlined(self, m);
2933a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
2934a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->kind = DeoptimizationRequest::kFullDeoptimization;
2935a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->method = nullptr;
2936a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
2937a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->kind = DeoptimizationRequest::kSelectiveDeoptimization;
2938a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->method = m;
2939a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
2940a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  } else {
2941a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is at least one breakpoint for this method: we don't need to deoptimize.
2942a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    req->kind = DeoptimizationRequest::kNothing;
2943a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    req->method = nullptr;
2944a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
2945a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    need_full_deoptimization = existing_breakpoint->need_full_deoptimization;
2946a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SanityCheckExistingBreakpoints(m, need_full_deoptimization);
2947138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2948a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
2949a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  gBreakpoints.push_back(Breakpoint(m, location->dex_pc, need_full_deoptimization));
2950a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": "
2951a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz             << gBreakpoints[gBreakpoints.size() - 1];
2952872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2953872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2954a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization
2955a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// request if we need to undeoptimize.
29564d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
2957ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(location->method_id);
2958a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  DCHECK(m != nullptr) << "No method for method id " << location->method_id;
2959138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2960a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
2961a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  bool need_full_deoptimization = false;
2962a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
2963a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == location->dex_pc) {
2964a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
2965a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      need_full_deoptimization = gBreakpoints[i].need_full_deoptimization;
2966a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      DCHECK_NE(need_full_deoptimization, Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
2967a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      gBreakpoints.erase(gBreakpoints.begin() + i);
2968a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      break;
2969138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2970138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2971a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m);
2972a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (existing_breakpoint == nullptr) {
2973a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is no more breakpoint on this method: we need to undeoptimize.
2974a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
2975a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // This method required full deoptimization: we need to undeoptimize everything.
2976a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->kind = DeoptimizationRequest::kFullUndeoptimization;
2977a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->method = nullptr;
2978a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
2979a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // This method required selective deoptimization: we need to undeoptimize only that method.
2980a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->kind = DeoptimizationRequest::kSelectiveUndeoptimization;
2981a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->method = m;
2982a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
2983a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  } else {
2984a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is at least one breakpoint for this method: we don't need to undeoptimize.
2985a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    req->kind = DeoptimizationRequest::kNothing;
2986a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    req->method = nullptr;
2987a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SanityCheckExistingBreakpoints(m, need_full_deoptimization);
29888696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
2989872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2990872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2991449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't
2992449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// cause suspension if the thread is the current thread.
2993449db33fafa29578df60e8a323f78d5eb6247e76Jeff Haoclass ScopedThreadSuspension {
2994449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao public:
299533e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers  ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id)
299652d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz      LOCKS_EXCLUDED(Locks::thread_list_lock_)
299733e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
2998449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      thread_(NULL),
2999449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_(JDWP::ERR_NONE),
3000449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      self_suspend_(false),
300133e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      other_suspend_(false) {
3002449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    ScopedObjectAccessUnchecked soa(self);
3003449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    {
3004449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
3005449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_ = DecodeThread(soa, thread_id, thread_);
3006449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
3007449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (error_ == JDWP::ERR_NONE) {
3008449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      if (thread_ == soa.Self()) {
3009449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        self_suspend_ = true;
3010449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      } else {
3011449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
3012449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        jobject thread_peer = gRegistry->GetJObject(thread_id);
3013449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        bool timed_out;
3014d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        Thread* suspended_thread = ThreadList::SuspendThreadByPeer(thread_peer, true, true,
3015d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                                                   &timed_out);
3016449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension);
3017449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        if (suspended_thread == NULL) {
3018449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          // Thread terminated from under us while suspending.
3019449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          error_ = JDWP::ERR_INVALID_THREAD;
3020449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        } else {
3021449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          CHECK_EQ(suspended_thread, thread_);
3022449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          other_suspend_ = true;
3023449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        }
3024449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      }
3025449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
3026449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3027449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3028449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* GetThread() const {
3029449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return thread_;
3030449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3031449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3032449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError GetError() const {
3033449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return error_;
3034449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3035449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3036449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ~ScopedThreadSuspension() {
3037449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (other_suspend_) {
3038449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      Runtime::Current()->GetThreadList()->Resume(thread_, true);
3039449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
3040449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
3041449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3042449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao private:
3043449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* thread_;
3044449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError error_;
3045449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool self_suspend_;
3046449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool other_suspend_;
3047449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao};
3048449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
3049221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size,
305000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::JdwpStepDepth step_depth) {
3051449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* self = Thread::Current();
3052449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ScopedThreadSuspension sts(self, thread_id);
3053449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  if (sts.GetError() != JDWP::ERR_NONE) {
3054449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return sts.GetError();
30552435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
30568696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
30572435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
30582435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Work out what Method* we're in, the current line number, and how deep the stack currently
30592435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // is for step-out.
30602435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
30612435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
30620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SingleStepStackVisitor : public StackVisitor {
306361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    explicit SingleStepStackVisitor(Thread* thread, SingleStepControl* single_step_control,
306461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz                                    int32_t* line_number)
3065b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
306661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        : StackVisitor(thread, NULL), single_step_control_(single_step_control),
306761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          line_number_(line_number) {
306861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      DCHECK_EQ(single_step_control_, thread->GetSingleStepControl());
306961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->method = NULL;
307061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->stack_depth = 0;
30718696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
3072ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
307300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
307400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
307500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
307661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      mirror::ArtMethod* m = GetMethod();
30770399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!m->IsRuntimeMethod()) {
307861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        ++single_step_control_->stack_depth;
307961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control_->method == NULL) {
3080ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache();
308161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->method = m;
308261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          *line_number_ = -1;
30832435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          if (dex_cache != NULL) {
30844445a7e3398a6143939168097a3aa275b734504dIan Rogers            const DexFile& dex_file = *dex_cache->GetDexFile();
308561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz            *line_number_ = dex_file.GetLineNumFromPC(m, GetDexPc());
30862435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          }
30878696433d1b3d8ba15288483b777edd888de69135Elliott Hughes        }
30888696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
3089530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
30908696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
309161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
309261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
309361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    int32_t* const line_number_;
30948696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  };
3095449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
309661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* const thread = sts.GetThread();
309761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepControl* const single_step_control = thread->GetSingleStepControl();
309861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
309961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  int32_t line_number = -1;
310061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepStackVisitor visitor(thread, single_step_control, &line_number);
31010399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
31028696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
31032435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
31042435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Find the dex_pc values that correspond to the current line, for line-based single-stepping.
31052435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
31062435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
31072435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  struct DebugCallbackContext {
3108bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    explicit DebugCallbackContext(SingleStepControl* single_step_control, int32_t line_number,
3109bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz                                  const DexFile::CodeItem* code_item)
3110bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      : single_step_control_(single_step_control), line_number_(line_number), code_item_(code_item),
311161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        last_pc_valid(false), last_pc(0) {
31122435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
31132435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
311461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) {
31152435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context);
311661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (static_cast<int32_t>(line_number) == context->line_number_) {
31172435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        if (!context->last_pc_valid) {
31182435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          // Everything from this address until the next line change is ours.
31192435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc = address;
31202435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc_valid = true;
31212435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
31222435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Otherwise, if we're already in a valid range for this line,
31232435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // just keep going (shouldn't really happen)...
31247934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      } else if (context->last_pc_valid) {  // and the line number is new
31252435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Add everything from the last entry up until here to the set
31262435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) {
312761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          context->single_step_control_->dex_pcs.insert(dex_pc);
31282435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
31292435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        context->last_pc_valid = false;
31302435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
31317934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return false;  // There may be multiple entries for any given line.
31322435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
31332435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
313461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    ~DebugCallbackContext() {
31352435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      // If the line number was the last in the position table...
31362435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      if (last_pc_valid) {
3137bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz        size_t end = code_item_->insns_size_in_code_units_;
31382435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) {
313961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->dex_pcs.insert(dex_pc);
31402435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
31412435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
31422435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
31432435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
314461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
314561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    const int32_t line_number_;
3146bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    const DexFile::CodeItem* const code_item_;
31472435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    bool last_pc_valid;
31482435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    uint32_t last_pc;
31492435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  };
315061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->dex_pcs.clear();
3151ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  mirror::ArtMethod* m = single_step_control->method;
315261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (!m->IsNative()) {
31533e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes    MethodHelper mh(m);
3154bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    const DexFile::CodeItem* const code_item = mh.GetCodeItem();
3155bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    DebugCallbackContext context(single_step_control, line_number, code_item);
3156bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
31573e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes                                    DebugCallbackContext::Callback, NULL, &context);
31583e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes  }
31592435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
31602435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
31612435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Everything else...
31622435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
31632435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
316461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_size = step_size;
316561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_depth = step_depth;
316661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->is_active = true;
31678696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
31682435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  if (VLOG_IS_ON(jdwp)) {
316961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step thread: " << *thread;
317061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step size: " << single_step_control->step_size;
317161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step depth: " << single_step_control->step_depth;
317261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->method);
317361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current line: " << line_number;
317461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->stack_depth;
31752435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step dex_pc values:";
3176bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    for (uint32_t dex_pc : single_step_control->dex_pcs) {
3177bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      VLOG(jdwp) << StringPrintf(" %#x", dex_pc);
31782435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
31792435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
31802435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
31812435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
3182872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3183872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
318461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertzvoid Dbg::UnconfigureStep(JDWP::ObjectId thread_id) {
318561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
318661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
318761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* thread;
318861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
318987118ed6f3f99e7df33214c277cf200a7b9a7499Sebastien Hertz  if (error == JDWP::ERR_NONE) {
319061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* single_step_control = thread->GetSingleStepControl();
319161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    DCHECK(single_step_control != nullptr);
3192bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    single_step_control->Clear();
319361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  }
3194872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3195872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
319645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughesstatic char JdwpTagToShortyChar(JDWP::JdwpTag tag) {
319745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  switch (tag) {
319845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    default:
319945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag);
320045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
320145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Primitives.
320245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BYTE:    return 'B';
320345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CHAR:    return 'C';
320445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_FLOAT:   return 'F';
320545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_DOUBLE:  return 'D';
320645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_INT:     return 'I';
320745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_LONG:    return 'J';
320845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_SHORT:   return 'S';
320945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_VOID:    return 'V';
321045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BOOLEAN: return 'Z';
321145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
321245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Reference types.
321345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_ARRAY:
321445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_OBJECT:
321545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_STRING:
321645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD:
321745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD_GROUP:
321845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_LOADER:
321945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_OBJECT:
322045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return 'L';
322145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  }
322245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes}
322345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
322488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id,
322588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                  JDWP::RefTypeId class_id, JDWP::MethodId method_id,
322600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  uint32_t arg_count, uint64_t* arg_values,
322700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* arg_types, uint32_t options,
322800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* pResultTag, uint64_t* pResultValue,
322900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::ObjectId* pExceptionId) {
3230d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  ThreadList* thread_list = Runtime::Current()->GetThreadList();
3231d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3232d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  Thread* targetThread = NULL;
3233d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  DebugInvokeReq* req = NULL;
323400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* self = Thread::Current();
3235d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
323600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(self);
323750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
3238221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, targetThread);
3239221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
3240221229cb523f849f165fdafbf9785010963715daElliott Hughes      LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id;
3241221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
3242d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3243d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    req = targetThread->GetInvokeReq();
3244d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (!req->ready) {
3245d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread;
3246d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      return JDWP::ERR_INVALID_THREAD;
3247d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3248d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3249d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3250d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We currently have a bug where we don't successfully resume the
3251d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * target thread if the suspend count is too deep.  We're expected to
3252d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * require one "resume" for each "suspend", but when asked to execute
3253d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a method we have to resume fully and then re-suspend it back to the
3254d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * same level.  (The easiest way to cause this is to type "suspend"
3255d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * multiple times in jdb.)
3256d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
3257d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * It's unclear what this means when the event specifies "resume all"
3258d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * and some threads are suspended more deeply than others.  This is
3259d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a rare problem, so for now we just prevent it from hanging forever
3260d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * by rejecting the method invocation request.  Without this, we will
3261d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * be stuck waiting on a suspended thread.
3262d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
326300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    int suspend_count;
326400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
326550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
326600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      suspend_count = targetThread->GetSuspendCount();
326700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
3268d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (suspend_count > 1) {
3269d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count;
32707934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return JDWP::ERR_THREAD_SUSPENDED;  // Probably not expected here.
3271d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3272d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
32733f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    JDWP::JdwpError status;
32742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id);
327564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (receiver == ObjectRegistry::kInvalidObject) {
32763f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
32773f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
327845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
32792dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* thread = gRegistry->Get<mirror::Object*>(thread_id);
328064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (thread == ObjectRegistry::kInvalidObject) {
32813f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
32823f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
328345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // TODO: check that 'thread' is actually a java.lang.Thread!
328445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
32852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_id, status);
328645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (c == NULL) {
32873f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return status;
32883f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
328945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
3290ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = FromMethodId(method_id);
329145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic() != (receiver == NULL)) {
329245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_INVALID_METHODID;
329345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
329445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic()) {
329545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (m->GetDeclaringClass() != c) {
329645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
329745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
329845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    } else {
329945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
330045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
330145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
330245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
330345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
330445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Check the argument list matches the method.
330545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    MethodHelper mh(m);
330645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (mh.GetShortyLength() - 1 != arg_count) {
330745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_ILLEGAL_ARGUMENT;
330845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
330945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    const char* shorty = mh.GetShorty();
33100920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes    const DexFile::TypeList* types = mh.GetParameterTypeList();
331145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    for (size_t i = 0; i < arg_count; ++i) {
331245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) {
331345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_ILLEGAL_ARGUMENT;
331445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
33150920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
33160920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      if (shorty[i + 1] == 'L') {
33170920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        // Did we really get an argument of an appropriate reference type?
33180920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        mirror::Class* parameter_type = mh.GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
33190920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i]);
33200920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        if (argument == ObjectRegistry::kInvalidObject) {
33210920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_INVALID_OBJECT;
33220920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
33230630ab5239a7d7be24dedbc3f66c822332446fc3Sebastien Hertz        if (argument != NULL && !argument->InstanceOf(parameter_type)) {
33240920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_ILLEGAL_ARGUMENT;
33250920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
33260920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
33270920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        // Turn the on-the-wire ObjectId into a jobject.
33280920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]);
33290920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        v.l = gRegistry->GetJObject(arg_values[i]);
33300920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      }
333145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
333245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
3333d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->receiver = receiver;
3334d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->thread = thread;
3335d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->klass = c;
3336d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->method = m;
3337d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_count = arg_count;
3338d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_values = arg_values;
3339d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->options = options;
3340d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->invoke_needed = true;
3341d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3342d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3343d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // The fact that we've released the thread list lock is a bit risky --- if the thread goes
3344d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // away we're sitting high and dry -- but we must release this before the ResumeAllThreads
3345d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // call, and it's unwise to hold it during WaitForSuspend.
3346d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3347d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
3348d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3349d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We change our (JDWP thread) status, which should be THREAD_RUNNING,
335081ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes     * so we can suspend for a GC if the invoke request causes us to
3351d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * run out of memory.  It's also a good idea to change it before locking
3352d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the invokeReq mutex, although that should never be held for long.
3353d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
335400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
3355d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
33564dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Transferring control to event thread";
3357d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    {
3358d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      MutexLock mu(self, req->lock);
3359d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3360d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
33614dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming all threads";
336200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers        thread_list->UndoDebuggerSuspensions();
3363d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      } else {
33644dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming event thread only";
3365d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes        thread_list->Resume(targetThread, true);
3366d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
3367d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3368d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      // Wait for the request to finish executing.
3369d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      while (req->invoke_needed) {
3370d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz        req->cond.Wait(self);
3371d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
3372d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
33734dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Control has returned from event thread";
3374d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3375d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* wait for thread to re-suspend itself */
3376df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom    SuspendThread(thread_id, false /* request_suspension */);
337700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
3378d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3379d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3380d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  /*
3381d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * Suspend the threads.  We waited for the target thread to suspend
3382d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * itself, so all we need to do is suspend the others.
3383d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   *
3384d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * The suspendAllThreads() call will double-suspend the event thread,
3385d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * so we want to resume the target thread once to keep the books straight.
3386d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   */
3387d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
338800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
33894dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Suspending all threads";
339000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    thread_list->SuspendAllForDebugger();
339100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
33924dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Resuming event thread to balance the count";
3393d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    thread_list->Resume(targetThread, true);
3394d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3395d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3396d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Copy the result.
3397d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pResultTag = req->result_tag;
3398d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (IsPrimitiveTag(req->result_tag)) {
3399f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = req->result_value.GetJ();
3400d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else {
3401f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = gRegistry->Add(req->result_value.GetL());
3402d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3403d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pExceptionId = req->exception;
3404d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  return req->error;
3405872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3406872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3407872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
340800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
3409d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
341081ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // We can be called while an exception is pending. We need
3411d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // to preserve that across the method invocation.
3412eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  StackHandleScope<4> hs(soa.Self());
3413eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  auto old_throw_this_object = hs.NewHandle<mirror::Object>(nullptr);
3414eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  auto old_throw_method = hs.NewHandle<mirror::ArtMethod>(nullptr);
3415eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  auto old_exception = hs.NewHandle<mirror::Throwable>(nullptr);
341662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  uint32_t old_throw_dex_pc;
341762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  {
341862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation old_throw_location;
341962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
3420eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    old_throw_this_object.Assign(old_throw_location.GetThis());
3421eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    old_throw_method.Assign(old_throw_location.GetMethod());
3422eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    old_exception.Assign(old_exception_obj);
342362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_dex_pc = old_throw_location.GetDexPc();
342462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->ClearException();
342562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
3426d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3427d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Translate the method through the vtable, unless the debugger wants to suppress it.
3428eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  Handle<mirror::ArtMethod> m(hs.NewHandle(pReq->method));
3429d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz  if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver != NULL) {
3430eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    mirror::ArtMethod* actual_method = pReq->klass->FindVirtualMethodForVirtualOrInterface(m.Get());
3431eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    if (actual_method != m.Get()) {
3432eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m.Get()) << " to " << PrettyMethod(actual_method);
3433eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier      m.Assign(actual_method);
343445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
3435d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3436eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m.Get())
3437d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " receiver=" << pReq->receiver
3438d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " arg_count=" << pReq->arg_count;
3439eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  CHECK(m.Get() != nullptr);
3440d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3441d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  CHECK_EQ(sizeof(jvalue), sizeof(uint64_t));
3442d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3443eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  pReq->result_value = InvokeWithJValues(soa, pReq->receiver, soa.EncodeMethod(m.Get()),
344453b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers                                         reinterpret_cast<jvalue*>(pReq->arg_values));
3445d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
344662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  mirror::Throwable* exception = soa.Self()->GetException(NULL);
344762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->ClearException();
344862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  pReq->exception = gRegistry->Add(exception);
3449eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  pReq->result_tag = BasicTagFromDescriptor(MethodHelper(m.Get()).GetShorty());
3450d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (pReq->exception != 0) {
345162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    VLOG(jdwp) << "  JDWP invocation returning with exception=" << exception
345262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        << " " << exception->Dump();
3453f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    pReq->result_value.SetJ(0);
3454d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else if (pReq->result_tag == JDWP::JT_OBJECT) {
3455d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* if no exception thrown, examine object result more closely */
34569837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    JDWP::JdwpTag new_tag = TagFromObject(soa, pReq->result_value.GetL());
3457d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (new_tag != pReq->result_tag) {
34584dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes      VLOG(jdwp) << "  JDWP promoted result from " << pReq->result_tag << " to " << new_tag;
3459d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      pReq->result_tag = new_tag;
3460d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3461d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3462d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3463d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * Register the object.  We don't actually need an ObjectId yet,
3464d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * but we do need to be sure that the GC won't move or discard the
3465d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * object when we switch out of RUNNING.  The ObjectId conversion
3466d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * will add the object to the "do not touch" list.
3467d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
3468d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We can't use the "tracked allocation" mechanism here because
3469d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the object is going to be handed off to a different thread.
3470d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
3471f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    gRegistry->Add(pReq->result_value.GetL());
3472d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3473d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3474eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier  if (old_exception.Get() != NULL) {
3475eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
347662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                         old_throw_dex_pc);
3477eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    soa.Self()->SetException(gc_safe_throw_location, old_exception.Get());
3478d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3479872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3480872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3481d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes/*
34824b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * "request" contains a full JDWP packet, possibly with multiple chunks.  We
3483f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * need to process each, accumulate the replies, and ship the whole thing
3484f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * back.
3485f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
3486f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * Returns "true" if we have a reply.  The reply buffer is newly allocated,
3487f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * and includes the chunk type/length, followed by the data.
3488f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
34893d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes * OLD-TODO: we currently assume that the request and reply include a single
3490f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * chunk.  If this becomes inconvenient we will need to adapt.
3491f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes */
34924b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesbool Dbg::DdmHandlePacket(JDWP::Request& request, uint8_t** pReplyBuf, int* pReplyLen) {
3493f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  Thread* self = Thread::Current();
3494f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  JNIEnv* env = self->GetJniEnv();
3495f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
34964b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t type = request.ReadUnsigned32("type");
34974b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t length = request.ReadUnsigned32("length");
34984b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
34994b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  // Create a byte[] corresponding to 'request'.
35004b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  size_t request_length = request.size();
35014b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length));
35026a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (dataArray.get() == NULL) {
35034b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    LOG(WARNING) << "byte[] allocation failed: " << request_length;
3504f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
3505f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3506f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
35074b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  env->SetByteArrayRegion(dataArray.get(), 0, request_length, reinterpret_cast<const jbyte*>(request.data()));
35084b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  request.Skip(request_length);
3509f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3510f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Run through and find all chunks.  [Currently just find the first.]
35116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ScopedByteArrayRO contents(env, dataArray.get());
35124b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (length != request_length) {
3513ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%zd)", length, request_length);
3514f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3515f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3516f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3517f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
3518eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
3519eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                                                                 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
35204b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                                                 type, dataArray.get(), 0, length));
3521f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (env->ExceptionCheck()) {
3522f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
3523f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionDescribe();
3524f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
3525f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3526f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3527f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
35286a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (chunk.get() == NULL) {
3529f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3530f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3531f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3532f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  /*
3533f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * Pull the pieces out of the chunk.  We copy the results into a
3534f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * newly-allocated buffer that the caller can free.  We don't want to
3535f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * continue using the Chunk object because nothing has a reference to it.
3536f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
3537f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * We could avoid this by returning type/data/offset/length and having
3538f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * the caller be aware of the object lifetime issues, but that
353981ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes   * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
3540f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * if we have responses for multiple chunks.
3541f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
3542f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * So we're pretty much stuck with copying data around multiple times.
3543f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   */
3544eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
35454b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
3546eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
3547eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
3548f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
35494dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length);
35506a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (length == 0 || replyData.get() == NULL) {
3551f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3552f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3553f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
35544b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  const int kChunkHdrLen = 8;
3555f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  uint8_t* reply = new uint8_t[length + kChunkHdrLen];
3556f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (reply == NULL) {
3557f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
3558f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3559f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3560f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 0, type);
3561f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 4, length);
35626a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
3563f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3564f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyBuf = reply;
3565f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyLen = length + kChunkHdrLen;
3566f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
35674b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length);
3568f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  return true;
3569872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3570872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3571a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::DdmBroadcast(bool connect) {
35724dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
357347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
357447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  Thread* self = Thread::Current();
357550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  if (self->GetState() != kRunnable) {
357650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
357750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    /* try anyway? */
357847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
357947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
358047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  JNIEnv* env = self->GetJniEnv();
358147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
3582eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
3583eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
3584eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            event);
358547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (env->ExceptionCheck()) {
358647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
358747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionDescribe();
358847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionClear();
358947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
359047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
359147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3592872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmConnected() {
3593a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(true);
3594872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3595872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3596872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmDisconnected() {
3597a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(false);
359847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = false;
359947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
360047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
360147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes/*
36028218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes * Send a notification when a thread starts, stops, or changes its name.
360347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes *
360447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * Because we broadcast the full set of threads when the notifications are
360547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * first enabled, it's possible for "thread" to be actively executing.
360647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes */
36078218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
360847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (!gDdmThreadNotification) {
360947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    return;
361047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
361147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
36128218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  if (type == CHUNK_TYPE("THDE")) {
36138218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    uint8_t buf[4];
3614d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Set4BE(&buf[0], t->GetThreadId());
36158218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
36168218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  } else {
36178218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
361800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
3619eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    StackHandleScope<1> hs(soa.Self());
3620eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    Handle<mirror::String> name(hs.NewHandle(t->GetThreadName(soa)));
3621eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    size_t char_count = (name.Get() != NULL) ? name->GetLength() : 0;
3622eb8167a4f4d27fce0530f6724ab8032610cd146bMathieu Chartier    const jchar* chars = (name.Get() != NULL) ? name->GetCharArray()->GetData() : NULL;
362347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
362421f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    std::vector<uint8_t> bytes;
3625d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Append4BE(bytes, t->GetThreadId());
3626545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    JDWP::AppendUtf16BE(bytes, chars, char_count);
362721f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
362821f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    Dbg::DdmSendChunk(type, bytes);
362947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
363047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
363147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
363247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::DdmSetThreadNotification(bool enable) {
363300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Enable/disable thread notifications.
363447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = enable;
363547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (enable) {
363600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Suspend the VM then post thread start notifications for all threads. Threads attaching will
363700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // see a suspension in progress and block until that ends. They then post their own start
363800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // notification.
363900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    SuspendVM();
364000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    std::list<Thread*> threads;
364150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
364200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
364350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu(self, *Locks::thread_list_lock_);
364400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      threads = Runtime::Current()->GetThreadList()->GetList();
364500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
364600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
364750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      ScopedObjectAccess soa(self);
364802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (Thread* thread : threads) {
364902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR"));
365000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
365100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
365200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ResumeVM();
365347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
365447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
365547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3656a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
3657c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (IsDebuggerActive()) {
3658dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier    ScopedObjectAccessUnchecked soa(Thread::Current());
3659cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers    JDWP::ObjectId id = gRegistry->Add(t->GetPeer());
36608218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR"));
366147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
36628218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  Dbg::DdmSendThreadNotification(t, type);
366347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
366447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
366547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadStart(Thread* t) {
3666a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
366747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
366847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
366947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadDeath(Thread* t) {
3670a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
3671872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3672872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
36738218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) {
36743bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(buf != NULL);
36753bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  iovec vec[1];
36763bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
36773bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_len = byte_count;
36783bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Dbg::DdmSendChunkV(type, vec, 1);
3679872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3680872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
368121f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) {
368221f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  DdmSendChunk(type, bytes.size(), &bytes[0]);
368321f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes}
368421f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes
3685f52935278fca8c7aa220543eef4544e3d1105d91Brian Carlstromvoid Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
36863bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (gJdwpState == NULL) {
36874dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type;
36883bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
3689cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    gJdwpState->DdmSendChunkV(type, iov, iov_count);
36903bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
3691872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3692872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3693767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesint Dbg::DdmHandleHpifChunk(HpifWhen when) {
3694767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when == HPIF_WHEN_NOW) {
36957162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(when);
3696767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return true;
3697767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3698767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3699767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
3700767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
3701767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3702767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3703767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3704767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  gDdmHpifWhen = when;
3705767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
3706767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3707767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3708767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesbool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
3709767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
3710767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
3711767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3712767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3713767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3714767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
3715767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
3716767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3717767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3718767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3719767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (native) {
3720767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhen = when;
3721767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhat = what;
3722767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  } else {
3723767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhen = when;
3724767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhat = what;
3725767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3726767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
3727767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3728767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
37297162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesvoid Dbg::DdmSendHeapInfo(HpifWhen reason) {
37307162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  // If there's a one-shot 'when', reset it.
37317162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  if (reason == gDdmHpifWhen) {
37327162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
37337162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes      gDdmHpifWhen = HPIF_WHEN_NEVER;
37347162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    }
37357162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  }
37367162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes
37377162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  /*
37387162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Chunk HPIF (client --> server)
37397162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
37407162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Heap Info. General information about the heap,
37417162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * suitable for a summary display.
37427162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
37437162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   [u4]: number of heaps
37447162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
37457162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   For each heap:
37467162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: heap ID
37477162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u8]: timestamp in ms since Unix epoch
37487162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u1]: capture reason (same as 'when' value from server)
37497162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: max heap size in bytes (-Xmx)
37507162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current heap size in bytes
37517162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of bytes allocated
37527162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of objects allocated
37537162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   */
37547162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  uint8_t heap_count = 1;
37551d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::Heap* heap = Runtime::Current()->GetHeap();
375621f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  std::vector<uint8_t> bytes;
3757545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append4BE(bytes, heap_count);
37587934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, 1);  // Heap id (bogus; we only have one heap).
3759545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append8BE(bytes, MilliTime());
3760545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append1BE(bytes, reason);
37617934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetMaxMemory());  // Max allowed heap size in bytes.
37627934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetTotalMemory());  // Current heap size in bytes.
3763b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetBytesAllocated());
3764b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetObjectsAllocated());
376521f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
376621f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes);
3767767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3768767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
37696a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgSolidity {
37706a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FREE = 0,
37716a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_HARD = 1,
37726a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SOFT = 2,
37736a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_WEAK = 3,
37746a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_PHANTOM = 4,
37756a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FINALIZABLE = 5,
37766a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SWEEP = 6,
37776a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
37786a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37796a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgKind {
37806a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_OBJECT = 0,
37816a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_CLASS_OBJECT = 1,
37826a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_1 = 2,
37836a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_2 = 3,
37846a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_4 = 4,
37856a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_8 = 5,
37866a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_UNKNOWN = 6,
37876a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_NATIVE = 7,
37886a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
37896a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37906a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_PARTIAL (1<<7)
37916a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
37926a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
379330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogersclass HeapChunkContext {
379430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers public:
37956a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Maximum chunk size.  Obtain this from the formula:
37966a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
37976a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  HeapChunkContext(bool merge, bool native)
379830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      : buf_(16384 - 16),
379930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        type_(0),
380030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        merge_(merge) {
38016a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
38026a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    if (native) {
380330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = CHUNK_TYPE("NHSG");
38046a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    } else {
380530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
38066a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
38076a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
38086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
38096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ~HeapChunkContext() {
381030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (p_ > &buf_[0]) {
38116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      Flush();
38126a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
38136a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
38146a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
38156a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void EnsureHeader(const void* chunk_ptr) {
381630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (!needHeader_) {
38176a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      return;
38186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
38196a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
38206a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Start a new HPSx chunk.
38217934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 1);  // Heap id (bogus; we only have one heap).
38227934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write1BE(&p_, 8);  // Size of allocation unit, in bytes.
38236a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
38247934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr));  // virtual address of segment start.
38257934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 0);  // offset of this piece (relative to the virtual address).
38266a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // [u4]: length of piece, in allocation units
38276a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // We won't know this until we're done, so save the offset and stuff in a dummy value.
382830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = p_;
382930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Write4BE(&p_, 0x55555555);
383030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = false;
38316a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
38326a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3833b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3834d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    if (pieceLenField_ == NULL) {
3835d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
3836d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      CHECK(needHeader_);
3837d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      return;
3838d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    }
38396a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Patch the "length of piece" field.
384030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(&buf_[0], pieceLenField_);
384130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(pieceLenField_, p_);
384230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Set4BE(pieceLenField_, totalAllocationUnits_);
38436a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
384430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]);
38456a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
38466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
38476a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
384800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg)
3849b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3850b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
385130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes);
3852a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
3853a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
38546a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes private:
3855a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  enum { ALLOCATION_UNIT_SIZE = 8 };
3856a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
38576a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void Reset() {
385830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    p_ = &buf_[0];
385915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    startOfNextMemoryChunk_ = NULL;
386030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    totalAllocationUnits_ = 0;
386130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = true;
386230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = NULL;
38636a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
38646a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
386500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void HeapChunkCallback(void* start, void* /*end*/, size_t used_bytes)
3866b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3867b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
386830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
386930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // in the following code not to allocate memory, by ensuring buf_ is of the correct size
387015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (used_bytes == 0) {
387115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start == NULL) {
387215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            // Reset for start of new heap.
387315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
387415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
387515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
387615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Only process in use memory so that free region information
387715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // also includes dlmalloc book keeping.
3878a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
3879a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
38806a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
388115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    /* If we're looking at the native heap, we'll just return
388215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     * (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks
388315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     */
388415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bool native = type_ == CHUNK_TYPE("NHSG");
388515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
388615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (startOfNextMemoryChunk_ != NULL) {
388715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Transmit any pending free memory. Native free memory of
388815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // over kMaxFreeLen could be because of the use of mmaps, so
388915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // don't report. If not free memory then start a new segment.
389015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        bool flush = true;
389115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start > startOfNextMemoryChunk_) {
389215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            const size_t kMaxFreeLen = 2 * kPageSize;
389315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeStart = startOfNextMemoryChunk_;
389415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeEnd = start;
38952d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom            size_t freeLen = reinterpret_cast<char*>(freeEnd) - reinterpret_cast<char*>(freeStart);
389615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            if (!native || freeLen < kMaxFreeLen) {
389715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), freeStart, freeLen);
389815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                flush = false;
389915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            }
390015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
390115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (flush) {
390215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
390315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
390415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
390515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
3906ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    mirror::Object* obj = reinterpret_cast<mirror::Object*>(start);
3907a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
3908a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Determine the type of this chunk.
3909a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
3910a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If it's the same, we should combine them.
391115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    uint8_t state = ExamineObject(obj, native);
391215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
391315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // allocation then the first sizeof(size_t) may belong to it.
391415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    const size_t dlMallocOverhead = sizeof(size_t);
391515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    AppendChunk(state, start, used_bytes + dlMallocOverhead);
39162d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom    startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + dlMallocOverhead;
391715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  }
391815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
391915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void AppendChunk(uint8_t state, void* ptr, size_t length)
3920b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
392115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // Make sure there's enough room left in the buffer.
392215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
392315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // 17 bytes for any header.
392415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t needed = (((length/ALLOCATION_UNIT_SIZE + 255) / 256) * 2) + 17;
392515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
392615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
392715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      Flush();
392815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
3929a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
393015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
393115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
393215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
393315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers          << needed << " bytes)";
393415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return;
393515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
393615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    EnsureHeader(ptr);
3937a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Write out the chunk description.
393815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    length /= ALLOCATION_UNIT_SIZE;   // Convert to allocation units.
393915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    totalAllocationUnits_ += length;
394015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    while (length > 256) {
394130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = state | HPSG_PARTIAL;
394230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = 255;     // length - 1
394315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      length -= 256;
3944a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
394530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    *p_++ = state;
394615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    *p_++ = length - 1;
39476a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
39486a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3949ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint8_t ExamineObject(mirror::Object* o, bool is_native_heap)
3950ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
3951a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (o == NULL) {
3952a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_FREE, 0);
3953a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
39546a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3955a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // It's an allocated chunk. Figure out what it is.
39566a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3957a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If we're looking at the native heap, we'll just return
3958a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks.
395900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (is_native_heap) {
3960a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
39616a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
39626a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39635bfa60ffcc953340feb711ed05cf576ac821905eIan Rogers    if (!Runtime::Current()->GetHeap()->IsLiveObjectLocked(o)) {
396415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
396500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
396600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
39672dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = o->GetClass();
3968a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c == NULL) {
3969a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      // The object was probably just created but hasn't been initialized yet.
3970a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
3971a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
39726a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3973590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(c)) {
397415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
3975a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
3976a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
39776a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3978a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsClassClass()) {
3979a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
39806a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
39816a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3982a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsArrayClass()) {
3983a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (o->IsObjectArray()) {
3984a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3985a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
3986a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      switch (c->GetComponentSize()) {
3987a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
3988a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
3989a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3990a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
3991a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
39926a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
39936a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3994a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
39956a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
39966a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
399730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  std::vector<uint8_t> buf_;
399830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* p_;
399930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* pieceLenField_;
400015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void* startOfNextMemoryChunk_;
400130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  size_t totalAllocationUnits_;
400230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint32_t type_;
400330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool merge_;
400430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool needHeader_;
400530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers
4006a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
4007a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes};
40086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
40096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesvoid Dbg::DdmSendHeapSegments(bool native) {
40106a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhen when;
40116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhat what;
40126a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (!native) {
40136a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmHpsgWhen;
40146a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmHpsgWhat;
40156a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  } else {
40166a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmNhsgWhen;
40176a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmNhsgWhat;
40186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
40196a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (when == HPSG_WHEN_NEVER) {
40206a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    return;
40216a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
40226a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
40236a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Figure out what kind of chunks we'll be sending.
40246a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what);
40256a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
40266a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // First, send a heap start chunk.
40276a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  uint8_t heap_id[4];
40287934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Set4BE(&heap_id[0], 1);  // Heap id (bogus; we only have one heap).
40296a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id);
40306a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4031cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Thread* self = Thread::Current();
4032cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
4033cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // To allow the Walk/InspectAll() below to exclusively-lock the
4034cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // mutator lock, temporarily release the shared access to the
4035cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // mutator lock here by transitioning to the suspended state.
4036cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Locks::mutator_lock_->AssertSharedHeld(self);
4037cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  self->TransitionFromRunnableToSuspended(kSuspended);
4038cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
40396a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Send a series of heap segment chunks.
4040a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native);
4041a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (native) {
4042c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris#ifdef USE_DLMALLOC
40431d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context);
4044c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris#else
4045c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris    UNIMPLEMENTED(WARNING) << "Native heap inspection is only supported with dlmalloc";
4046c4ddc042eaf5232a3f9b111f42af39eeab6e0294Christopher Ferris#endif
4047a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
40481d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
40491d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
40501d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
40511d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    for (It cur = spaces.begin(), end = spaces.end(); cur != end; ++cur) {
4052cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      if ((*cur)->IsMallocSpace()) {
4053cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi        (*cur)->AsMallocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
4054b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier      }
4055b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
4056e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    // Walk the large objects, these are not in the AllocSpace.
4057e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
4058a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
40596a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
4060cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Shared-lock the mutator lock back.
4061cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  self->TransitionFromSuspendedToRunnable();
4062cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Locks::mutator_lock_->AssertSharedHeld(self);
4063cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
40646a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Finally, send a heap end chunk.
40656a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id);
4066767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
4067767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4068b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughesstatic size_t GetAllocTrackerMax() {
4069b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#ifdef HAVE_ANDROID_OS
4070b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  // Check whether there's a system property overriding the number of records.
4071b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  const char* propertyName = "dalvik.vm.allocTrackerMax";
4072b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  char allocRecordMaxString[PROPERTY_VALUE_MAX];
4073b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  if (property_get(propertyName, allocRecordMaxString, "") > 0) {
4074b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    char* end;
4075b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    size_t value = strtoul(allocRecordMaxString, &end, 10);
4076b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (*end != '\0') {
40773e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
40783e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- invalid";
4079b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
4080b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
4081b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (!IsPowerOfTwo(value)) {
40823e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
40833e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- not power of two";
4084b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
4085b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
4086b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    return value;
4087b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  }
4088b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#endif
4089b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  return kDefaultNumAllocRecords;
4090b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes}
4091b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes
4092545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::SetAllocTrackingEnabled(bool enabled) {
4093545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (enabled) {
4094b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    {
4095b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
4096b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      if (recent_allocation_records_ == NULL) {
4097b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        alloc_record_max_ = GetAllocTrackerMax();
4098b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        LOG(INFO) << "Enabling alloc tracker (" << alloc_record_max_ << " entries of "
4099b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz            << kMaxAllocRecordStackDepth << " frames, taking "
4100b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz            << PrettySize(sizeof(AllocRecord) * alloc_record_max_) << ")";
4101b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        alloc_record_head_ = alloc_record_count_ = 0;
4102b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        recent_allocation_records_ = new AllocRecord[alloc_record_max_];
4103b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        CHECK(recent_allocation_records_ != NULL);
4104b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      }
4105545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4106fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers    Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints();
4107545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  } else {
4108fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers    Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
4109b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    {
4110b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
4111b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      delete[] recent_allocation_records_;
4112b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      recent_allocation_records_ = NULL;
4113b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    }
4114545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4115545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4116545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
41170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct AllocRecordStackVisitor : public StackVisitor {
41187a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor(Thread* thread, AllocRecord* record)
4119b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
41207a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, NULL), record(record), depth(0) {}
4121545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
412200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
412300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
412400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
4125545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if (depth >= kMaxAllocRecordStackDepth) {
4126530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return false;
4127545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4128ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = GetMethod();
41290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    if (!m->IsRuntimeMethod()) {
41300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].method = m;
41310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].dex_pc = GetDexPc();
4132530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      ++depth;
4133545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4134530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes    return true;
4135545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4136545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4137545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  ~AllocRecordStackVisitor() {
4138545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // Clear out any unused stack trace elements.
4139545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (; depth < kMaxAllocRecordStackDepth; ++depth) {
4140545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      record->stack[depth].method = NULL;
41410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].dex_pc = 0;
4142545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4143545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4144545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4145545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  AllocRecord* record;
4146545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t depth;
4147545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
4148545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
41492dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::RecordAllocation(mirror::Class* type, size_t byte_count) {
4150545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  Thread* self = Thread::Current();
4151545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  CHECK(self != NULL);
4152545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4153719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  MutexLock mu(self, *alloc_tracker_lock_);
4154545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
4155545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
4156545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4157545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4158545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Advance and clip.
4159719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (++alloc_record_head_ == alloc_record_max_) {
4160719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    alloc_record_head_ = 0;
4161545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4162545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4163545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the basics.
4164719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  AllocRecord* record = &recent_allocation_records_[alloc_record_head_];
4165545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  record->type = type;
4166545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  record->byte_count = byte_count;
4167d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  record->thin_lock_id = self->GetThreadId();
4168545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4169545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the stack trace.
41707a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor visitor(self, record);
41710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
4172545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4173719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (alloc_record_count_ < alloc_record_max_) {
4174719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    ++alloc_record_count_;
4175545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4176545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4177545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4178a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// Returns the index of the head element.
4179a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
4180a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We point at the most-recently-written record, so if gAllocRecordCount is 1
4181a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// we want to use the current element.  Take "head+1" and subtract count
4182a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// from it.
4183a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
4184a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We need to handle underflow in our circular buffer, so we add
4185b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes// gAllocRecordMax and then mask it back down.
4186719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::HeadIndex() {
4187719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  return (Dbg::alloc_record_head_ + 1 + Dbg::alloc_record_max_ - Dbg::alloc_record_count_) &
4188719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      (Dbg::alloc_record_max_ - 1);
4189545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4190545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4191545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::DumpRecentAllocations() {
419200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
4193719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  MutexLock mu(soa.Self(), *alloc_tracker_lock_);
4194545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
4195545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    LOG(INFO) << "Not recording tracked allocations";
4196545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
4197545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4198545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4199545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // "i" is the head of the list.  We want to start at the end of the
4200545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // list and move forward to the tail.
4201a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t i = HeadIndex();
4202719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  size_t count = alloc_record_count_;
4203545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4204719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  LOG(INFO) << "Tracked allocations, (head=" << alloc_record_head_ << " count=" << count << ")";
4205545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  while (count--) {
4206545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    AllocRecord* record = &recent_allocation_records_[i];
4207545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4208a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->thin_lock_id, record->byte_count)
4209545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes              << PrettyClass(record->type);
4210545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4211545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
4212ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      mirror::ArtMethod* m = record->stack[stack_frame].method;
4213545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      if (m == NULL) {
4214545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes        break;
4215545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
4216545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      LOG(INFO) << "    " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber();
4217545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4218545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4219545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // pause periodically to help logcat catch up
4220545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if ((count % 5) == 0) {
4221545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      usleep(40000);
4222545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4223545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4224719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    i = (i + 1) & (alloc_record_max_ - 1);
4225545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4226545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4227545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
42283b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid Dbg::UpdateObjectPointers(IsMarkedCallback* callback, void* arg) {
4229719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (recent_allocation_records_ != nullptr) {
4230719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
4231719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    size_t i = HeadIndex();
4232719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    size_t count = alloc_record_count_;
4233719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    while (count--) {
4234719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      AllocRecord* record = &recent_allocation_records_[i];
4235719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      DCHECK(record != nullptr);
42363b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier      record->UpdateObjectPointers(callback, arg);
4237719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      i = (i + 1) & (alloc_record_max_ - 1);
4238412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    }
4239412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
4240412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (gRegistry != nullptr) {
42413b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    gRegistry->UpdateObjectPointers(callback, arg);
4242412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
4243412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier}
4244412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
4245412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartiervoid Dbg::AllowNewObjectRegistryObjects() {
4246412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (gRegistry != nullptr) {
4247412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    gRegistry->AllowNewObjects();
4248412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
4249412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier}
4250412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
4251412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartiervoid Dbg::DisallowNewObjectRegistryObjects() {
4252412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (gRegistry != nullptr) {
4253412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    gRegistry->DisallowNewObjects();
4254412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
4255412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier}
4256412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
4257545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesclass StringTable {
4258545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes public:
4259545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  StringTable() {
4260545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4261545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
42626d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  void Add(const char* s) {
4263545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    table_.insert(s);
4264545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4265545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4266a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t IndexOf(const char* s) const {
426702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    auto it = table_.find(s);
4268a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    if (it == table_.end()) {
4269a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes      LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
4270a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    }
4271a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    return std::distance(table_.begin(), it);
4272545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4273545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4274a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t Size() const {
4275545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return table_.size();
4276545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4277545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4278a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  void WriteTo(std::vector<uint8_t>& bytes) const {
427902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const std::string& str : table_) {
428002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      const char* s = str.c_str();
42816d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      size_t s_len = CountModifiedUtf8Chars(s);
4282700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers      std::unique_ptr<uint16_t> s_utf16(new uint16_t[s_len]);
42836d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      ConvertModifiedUtf8ToUtf16(s_utf16.get(), s);
42846d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len);
4285545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4286545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4287545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4288545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes private:
4289a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  std::set<std::string> table_;
4290545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(StringTable);
4291545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
4292545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4293280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertzstatic const char* GetMethodSourceFile(MethodHelper* mh)
4294280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4295280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz  DCHECK(mh != nullptr);
4296280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz  const char* source_file = mh->GetDeclaringClassSourceFile();
4297280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz  return (source_file != nullptr) ? source_file : "";
4298280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz}
4299280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz
4300545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes/*
4301545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The data we send to DDMS contains everything we have recorded.
4302545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4303545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * Message header (all values big-endian):
4304545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) message header len (to allow future expansion); includes itself
4305545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) entry header len
4306545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) stack frame len
4307545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of entries
4308545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (4b) offset to string table from start of message
4309545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of class name strings
4310545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of method name strings
4311545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of source file name strings
4312545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * For each entry:
4313545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (4b) total allocation size
4314221229cb523f849f165fdafbf9785010963715daElliott Hughes *   (2b) thread id
4315545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (2b) allocated object's class name index
4316545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (1b) stack depth
4317545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   For each stack frame:
4318545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method's class name
4319545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method name
4320545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method source file
4321545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) line number, clipped to 32767; -2 if native; -1 if no source
4322545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) class name strings
4323545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) method name strings
4324545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) source file strings
4325545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4326545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * As with other DDM traffic, strings are sent as a 4-byte length
4327545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * followed by UTF-16 data.
4328545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4329545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We send up 16-bit unsigned indexes into string tables.  In theory there
4330b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes * can be (kMaxAllocRecordStackDepth * gAllocRecordMax) unique strings in
4331545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * each table, but in practice there should be far fewer.
4332545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4333545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The chief reason for using a string table here is to keep the size of
4334545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * the DDMS message to a minimum.  This is partly to make the protocol
4335545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * efficient, but also because we have to form the whole thing up all at
4336545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * once in a memory buffer.
4337545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4338545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We use separate string tables for class names, method names, and source
4339545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * files to keep the indexes small.  There will generally be no overlap
4340545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * between the contents of these tables.
4341545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes */
4342545a064aca775ba801790fced3d713d8a87bfc61Elliott HughesjbyteArray Dbg::GetRecentAllocations() {
4343545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (false) {
4344545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    DumpRecentAllocations();
4345545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4346545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
434750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* self = Thread::Current();
434846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  std::vector<uint8_t> bytes;
434946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  {
4350719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    MutexLock mu(self, *alloc_tracker_lock_);
435146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
435246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 1: generate string tables.
435346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
435446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable class_names;
435546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable method_names;
435646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable filenames;
435746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4358719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    int count = alloc_record_count_;
435946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    int idx = HeadIndex();
436046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
436146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
436246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4363f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier      class_names.Add(record->type->GetDescriptor().c_str());
436446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
436546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      MethodHelper mh;
436646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) {
4367ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        mirror::ArtMethod* m = record->stack[i].method;
436846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        if (m != NULL) {
436946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          mh.ChangeMethod(m);
437046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          class_names.Add(mh.GetDeclaringClassDescriptor());
437146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          method_names.Add(mh.GetName());
4372280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz          filenames.Add(GetMethodSourceFile(&mh));
437346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        }
437446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      }
4375545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4376719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      idx = (idx + 1) & (alloc_record_max_ - 1);
437746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    }
437846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4379719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    LOG(INFO) << "allocation records: " << alloc_record_count_;
438046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
438146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
438246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 2: Generate the output and store it in the buffer.
438346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
438446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
438546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) message header len (to allow future expansion); includes itself
438646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) entry header len
438746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) stack frame len
438846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kMessageHeaderLen = 15;
438946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kEntryHeaderLen = 9;
439046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kStackFrameLen = 8;
439146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kMessageHeaderLen);
439246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kEntryHeaderLen);
439346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kStackFrameLen);
439446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
439546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of entries
439646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (4b) offset to string table from start of message
439746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of class name strings
439846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of method name strings
439946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of source file name strings
4400719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    JDWP::Append2BE(bytes, alloc_record_count_);
440146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    size_t string_table_offset = bytes.size();
44027934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Append4BE(bytes, 0);  // We'll patch this later...
440346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, class_names.Size());
440446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, method_names.Size());
440546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, filenames.Size());
440646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4407719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    count = alloc_record_count_;
440846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    idx = HeadIndex();
440946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
441046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // For each entry:
441146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (4b) total allocation size
441246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) thread id
441346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) allocated object's class name index
441446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (1b) stack depth
441546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
441646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      size_t stack_depth = record->GetDepth();
4417f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier      size_t allocated_object_class_name_index =
4418f832284dd847ff077577bb5712225430bbbb3b67Mathieu Chartier          class_names.IndexOf(record->type->GetDescriptor().c_str());
441946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append4BE(bytes, record->byte_count);
442046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, record->thin_lock_id);
442146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, allocated_object_class_name_index);
442246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append1BE(bytes, stack_depth);
442346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
442446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      MethodHelper mh;
442546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
442646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // For each stack frame:
442746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method's class name
442846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method name
442946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method source file
443046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) line number, clipped to 32767; -2 if native; -1 if no source
443146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        mh.ChangeMethod(record->stack[stack_frame].method);
443246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        size_t class_name_index = class_names.IndexOf(mh.GetDeclaringClassDescriptor());
443346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        size_t method_name_index = method_names.IndexOf(mh.GetName());
4434280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz        size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(&mh));
443546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, class_name_index);
443646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, method_name_index);
443746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, file_name_index);
443846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber());
4439545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
444046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4441719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      idx = (idx + 1) & (alloc_record_max_ - 1);
4442545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4443545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
444446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) class name strings
444546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) method name strings
444646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) source file strings
444746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Set4BE(&bytes[string_table_offset], bytes.size());
444846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    class_names.WriteTo(bytes);
444946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    method_names.WriteTo(bytes);
445046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    filenames.WriteTo(bytes);
4451545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
445250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  JNIEnv* env = self->GetJniEnv();
4453545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  jbyteArray result = env->NewByteArray(bytes.size());
4454545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (result != NULL) {
4455545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
4456545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4457545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  return result;
4458545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4459545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4460872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}  // namespace art
4461