debugger.cc revision 280286ac8a0e3a30c68be511c8b7a0a4d62936d7
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"
3164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "jdwp/object_registry.h"
32ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_field-inl.h"
33ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.h"
342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class.h"
352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class_loader.h"
372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/throwable.h"
406d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h"
41a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz#include "quick/inline_method_analyser.h"
4253b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers#include "reflection.h"
43a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include "safe_map.h"
4464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "scoped_thread_state_change.h"
456a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#include "ScopedLocalRef.h"
46f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes#include "ScopedPrimitiveArray.h"
471f5393447b9f45be7918042d9ee7b521376de866Ian Rogers#include "sirt_ref.h"
4847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes#include "stack_indirect_reference_table.h"
49475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes#include "thread_list.h"
5062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers#include "throw_location.h"
512dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "utf.h"
52a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz#include "verifier/method_verifier-inl.h"
53eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "well_known_classes.h"
54475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
553d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#ifdef HAVE_ANDROID_OS
563d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#include "cutils/properties.h"
573d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#endif
583d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom
59872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesnamespace art {
60872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
617934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstromstatic const size_t kMaxAllocRecordStackDepth = 16;  // Max 255.
627934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstromstatic const size_t kDefaultNumAllocRecords = 64*1024;  // Must be a power of 2.
63475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
64545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstruct AllocRecordStackTraceElement {
65ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* method;
660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  uint32_t dex_pc;
67545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
68412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  AllocRecordStackTraceElement() : method(nullptr), dex_pc(0) {
69412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
70412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
71b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  int32_t LineNumber() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return MethodHelper(method).GetLineNumFromDexPC(dex_pc);
73545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
74545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
75545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
76545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstruct AllocRecord {
772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* type;
78545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t byte_count;
79545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  uint16_t thin_lock_id;
807934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  AllocRecordStackTraceElement stack[kMaxAllocRecordStackDepth];  // Unused entries have NULL method.
81545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
82545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t GetDepth() {
83545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    size_t depth = 0;
84545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    while (depth < kMaxAllocRecordStackDepth && stack[depth].method != NULL) {
85545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      ++depth;
86545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
87545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return depth;
88545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
89412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
9083c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier  void UpdateObjectPointers(IsMarkedCallback* callback, void* arg)
91412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
92412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    if (type != nullptr) {
9383c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier      type = down_cast<mirror::Class*>(callback(type, arg));
94412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    }
95412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
96412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier      mirror::ArtMethod*& m = stack[stack_frame].method;
97412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier      if (m == nullptr) {
98412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier        break;
99412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier      }
10083c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier      m = down_cast<mirror::ArtMethod*>(callback(m, arg));
101412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    }
102412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
103545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
104545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
1058696433d1b3d8ba15288483b777edd888de69135Elliott Hughesstruct Breakpoint {
106a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  // The location of this breakpoint.
107ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* method;
108a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  uint32_t dex_pc;
109a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
110a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  // Indicates whether breakpoint needs full deoptimization or selective deoptimization.
111a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  bool need_full_deoptimization;
112a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
113a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  Breakpoint(mirror::ArtMethod* method, uint32_t dex_pc, bool need_full_deoptimization)
114a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    : method(method), dex_pc(dex_pc), need_full_deoptimization(need_full_deoptimization) {}
1153b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
1163b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  void VisitRoots(RootCallback* callback, void* arg) {
1173b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    if (method != nullptr) {
1183b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier      callback(reinterpret_cast<mirror::Object**>(&method), arg, 0, kRootDebugger);
1193b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    }
1203b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
1218696433d1b3d8ba15288483b777edd888de69135Elliott Hughes};
1228696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
12300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs)
124b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
125229feb7a09317919ee51c06d1c3e715cea25da75Elliott Hughes  os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.method).c_str(), rhs.dex_pc);
1268696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return os;
1278696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
1288696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
1293f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzclass DebugInstrumentationListener FINAL : public instrumentation::InstrumentationListener {
13062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers public:
13162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  DebugInstrumentationListener() {}
13262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual ~DebugInstrumentationListener() {}
13362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1343f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodEntered(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1353f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                     uint32_t dex_pc)
1363f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
13762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
13862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
13962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
14062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
141579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    Dbg::PostLocationEvent(method, 0, this_object, Dbg::kMethodEntry, nullptr);
14262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
14362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1443f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodExited(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1453f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc, const JValue& return_value)
1463f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
14762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
14862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
14962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
15062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
151579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    Dbg::PostLocationEvent(method, dex_pc, this_object, Dbg::kMethodExit, &return_value);
15262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
15362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1543f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodUnwind(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1553f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc)
1563f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
15762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    // We're not recorded to listen to this kind of event, so complain.
15862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method)
15951db44a194bafc3810a41164a8b39614f10e79dfSebastien Hertz               << " " << dex_pc;
16062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
16162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1623f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void DexPcMoved(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1633f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                  uint32_t new_dex_pc)
1643f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc);
16662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
16762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1683f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void FieldRead(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1693f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                 uint32_t dex_pc, mirror::ArtField* field)
1703f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1713f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostFieldAccessEvent(method, dex_pc, this_object, field);
17262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
1733f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
1743f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void FieldWritten(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1753f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc, mirror::ArtField* field, const JValue& field_value)
1763f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1773f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostFieldModificationEvent(method, dex_pc, this_object, field, &field_value);
1783f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
1793f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
1803f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
1813f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                       mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
1823f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                       mirror::Throwable* exception_object)
1833f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1843f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostException(throw_location, catch_method, catch_dex_pc, exception_object);
1853f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
1863f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
1873f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz private:
1883f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener);
18962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers} gDebugInstrumentationListener;
19062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1914ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes// JDWP is allowed unless the Zygote forbids it.
1924ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesstatic bool gJdwpAllowed = true;
1934ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
194c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line?
1953bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gJdwpConfigured = false;
1963bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
197c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Broken-down JDWP options. (Only valid if IsJdwpConfigured() is true.)
198376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughesstatic JDWP::JdwpOptions gJdwpOptions;
1993bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
2003bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes// Runtime JDWP state.
2013bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic JDWP::JdwpState* gJdwpState = NULL;
2023bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerConnected;  // debugger or DDMS is connected.
2033bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerActive;     // debugger is making requests.
2048696433d1b3d8ba15288483b777edd888de69135Elliott Hughesstatic bool gDisposed;           // debugger called VirtualMachine.Dispose, so we should drop the connection.
2053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
20647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesstatic bool gDdmThreadNotification = false;
20747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
208767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes// DDMS GC-related settings.
209767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
210767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
211767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmHpsgWhat;
212767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
213767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmNhsgWhat;
214767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
215719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstatic ObjectRegistry* gRegistry = nullptr;
216475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
217545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes// Recent allocation tracking.
218719d1a33f6569864f529e5a3fff59e7bca97aad0Ian RogersMutex* Dbg::alloc_tracker_lock_ = nullptr;
219719d1a33f6569864f529e5a3fff59e7bca97aad0Ian RogersAllocRecord* Dbg::recent_allocation_records_ = nullptr;  // TODO: CircularBuffer<AllocRecord>
220719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_max_ = 0;
221719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_head_ = 0;
222719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_count_ = 0;
223545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
224138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz// Deoptimization support.
2254d25df3f76f864b7629ac8c0046d46997f293d8dSebastien HertzMutex* Dbg::deoptimization_lock_ = nullptr;
2264d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzstd::vector<DeoptimizationRequest> Dbg::deoptimization_requests_;
2274d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzsize_t Dbg::full_deoptimization_event_count_ = 0;
2287ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzsize_t Dbg::delayed_full_undeoptimization_count_ = 0;
229138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
230138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz// Breakpoints.
23109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhaostatic std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_);
2328696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2333b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid DebugInvokeReq::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
2343b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier                                RootType root_type) {
2353b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (receiver != nullptr) {
2363b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(&receiver, arg, tid, root_type);
2373b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2383b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (thread != nullptr) {
2393b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(&thread, arg, tid, root_type);
2403b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2413b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (klass != nullptr) {
2423b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&klass), arg, tid, root_type);
2433b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2443b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (method != nullptr) {
2453b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&method), arg, tid, root_type);
2463b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2473b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
2483b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
249bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzvoid DebugInvokeReq::Clear() {
250bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  invoke_needed = false;
251bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  receiver = nullptr;
252bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  thread = nullptr;
253bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  klass = nullptr;
254bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  method = nullptr;
255bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
256bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
2573b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid SingleStepControl::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
2583b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier                                   RootType root_type) {
2593b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (method != nullptr) {
2603b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&method), arg, tid, root_type);
2613b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2623b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
2633b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
264bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzbool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const {
265bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  return dex_pcs.find(dex_pc) == dex_pcs.end();
266bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
267bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
268bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzvoid SingleStepControl::Clear() {
269bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  is_active = false;
270bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  method = nullptr;
271bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  dex_pcs.clear();
272bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
273bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
2744d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid DeoptimizationRequest::VisitRoots(RootCallback* callback, void* arg) {
2754d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  if (method != nullptr) {
2764d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    callback(reinterpret_cast<mirror::Object**>(&method), arg, 0, kRootDebugger);
2774d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
2784d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz}
2794d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz
280ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic bool IsBreakpoint(const mirror::ArtMethod* m, uint32_t dex_pc)
28109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    LOCKS_EXCLUDED(Locks::breakpoint_lock_)
282b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
28309bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
284138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
285a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes    if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == dex_pc) {
2868696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i];
2878696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      return true;
2888696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
2898696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
2908696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return false;
2918696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
2928696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
29352d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertzstatic bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread)
29452d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) {
2959e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
2969e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // A thread may be suspended for GC; in this code, we really want to know whether
2979e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // there's a debugger suspension active.
2989e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0;
2999e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes}
3009e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
3012dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status)
302b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3032dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
30464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
305436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
306436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
307436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
308436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsArrayInstance()) {
309436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_ARRAY;
310436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
311436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
312436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
313436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsArray();
314436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
315436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
3162dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError& status)
317b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3182dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
31964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
320436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
321436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
322436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
323436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsClass()) {
324436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_CLASS;
325436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
326436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
327436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
328436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsClass();
329436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
330436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
331221229cb523f849f165fdafbf9785010963715daElliott Hughesstatic JDWP::JdwpError DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, Thread*& thread)
332a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
333b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
334b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_peer = gRegistry->Get<mirror::Object*>(thread_id);
33664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_peer == NULL || thread_peer == ObjectRegistry::kInvalidObject) {
337221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't even an object.
338221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_OBJECT;
339221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
340221229cb523f849f165fdafbf9785010963715daElliott Hughes
3412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
342221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) {
343221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't a thread.
344221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_THREAD;
345221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
346221229cb523f849f165fdafbf9785010963715daElliott Hughes
347221229cb523f849f165fdafbf9785010963715daElliott Hughes  thread = Thread::FromManagedThread(soa, thread_peer);
348221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (thread == NULL) {
349221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This is a java.lang.Thread without a Thread*. Must be a zombie.
350221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_THREAD_NOT_ALIVE;
351436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
352221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
353436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
354436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
35524437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) {
35624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // JDWP deliberately uses the descriptor characters' ASCII values for its enum.
35724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // Note that by "basic" we mean that we don't get more specific than JT_OBJECT.
35824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  return static_cast<JDWP::JdwpTag>(descriptor[0]);
35924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
36024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
3619837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogersstatic JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c)
362b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
36386b0010c79ef95b5333cd540b7d3af34a9f1a643Elliott Hughes  CHECK(c != NULL);
36424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsArrayClass()) {
36524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_ARRAY;
36624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
36724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsStringClass()) {
36824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_STRING;
3699837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
3709837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  if (c->IsClassClass()) {
37124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_CLASS_OBJECT;
37224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
3739837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
3749837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* thread_class = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
3759837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (thread_class->IsAssignableFrom(c)) {
3769837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_THREAD;
3779837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
3789837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
3799837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
3809837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* thread_group_class =
3819837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
3829837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (thread_group_class->IsAssignableFrom(c)) {
3839837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_THREAD_GROUP;
3849837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
3859837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
3869837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
3879837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* class_loader_class =
3889837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader);
3899837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (class_loader_class->IsAssignableFrom(c)) {
3909837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_CLASS_LOADER;
3919837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
3929837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
3939837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  return JDWP::JT_OBJECT;
39424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
39524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
39624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes/*
39724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Objects declared to hold Object might actually hold a more specific
39824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * type.  The debugger may take a special interest in these (e.g. it
39924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * wants to display the contents of Strings), so we want to return an
40024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * appropriate tag.
40124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes *
40224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Null objects are tagged JT_OBJECT.
40324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes */
4049837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogersstatic JDWP::JdwpTag TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o)
405b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4069837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass());
40724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
40824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
40924437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic bool IsPrimitiveTag(JDWP::JdwpTag tag) {
41024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  switch (tag) {
41124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BOOLEAN:
41224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BYTE:
41324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_CHAR:
41424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_FLOAT:
41524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_DOUBLE:
41624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_INT:
41724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_LONG:
41824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_SHORT:
41924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_VOID:
42024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return true;
42124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  default:
42224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return false;
42324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
42424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
42524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
4263bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
4273bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Handle one of the JDWP name/value pairs.
4283bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
4293bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * JDWP options are:
4303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  help: if specified, show help message and bail
4313bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  transport: may be dt_socket or dt_shmem
4323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  address: for dt_socket, "host:port", or just "port" when listening
4333bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  server: if "y", wait for debugger to attach; if "n", attach to debugger
4343bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  timeout: how long to wait for debugger to connect / listen
4353bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
4363bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Useful with server=n (these aren't supported yet):
4373bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onthrow=<exception-name>: connect to debugger when exception thrown
4383bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onuncaught=y|n: connect to debugger when uncaught exception thrown
4393bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  launch=<command-line>: launch the debugger itself
4403bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
4413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * The "transport" option is required, as is "address" if server=n.
4423bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
4433bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool ParseJdwpOption(const std::string& name, const std::string& value) {
4443bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (name == "transport") {
4453bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "dt_socket") {
446376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
4473bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "dt_android_adb") {
448376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
4493bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
4503bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP transport not supported: " << value;
4513bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4523bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4533bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "server") {
4543bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
455376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = false;
4563bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
457376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = true;
4583bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
4593bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
4603bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4613bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4623bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "suspend") {
4633bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
464376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = false;
4653bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
466376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = true;
4673bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
4683bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
4693bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4703bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4713bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "address") {
4723bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* this is either <port> or <host>:<port> */
4733bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string port_string;
474376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.host.clear();
4753bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type colon = value.find(':');
4763bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (colon != std::string::npos) {
477376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.host = value.substr(0, colon);
4783bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value.substr(colon + 1);
4793bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
4803bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value;
4813bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4823bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (port_string.empty()) {
4833bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address missing port: " << value;
4843bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4853bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4863bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    char* end;
487ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    uint64_t port = strtoul(port_string.c_str(), &end, 10);
488ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    if (*end != '\0' || port > 0xffff) {
4893bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address has junk in port field: " << value;
4903bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4913bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
492376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.port = port;
4933bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
4943bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* valid but unsupported */
4953bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
4963bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
4973bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
4983bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
4993bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5003bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
5013bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
5023bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5033bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
5043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
5053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * "transport=dt_socket,address=8000,server=y,suspend=n"
5063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
5073bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesbool Dbg::ParseJdwpOptions(const std::string& options) {
5084dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "ParseJdwpOptions: " << options;
50947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
5103bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  std::vector<std::string> pairs;
5113bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Split(options, ',', pairs);
5123bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5133bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  for (size_t i = 0; i < pairs.size(); ++i) {
5143bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type equals = pairs[i].find('=');
5153bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (equals == std::string::npos) {
5163bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
5173bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5183bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5193bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
5203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
5213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
522376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
5233bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP transport: " << options;
5243bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
525376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
5263bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
5273bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    return false;
5283bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
5293bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gJdwpConfigured = true;
5313bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
5323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
5333bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
534d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StartJdwp() {
535c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!gJdwpAllowed || !IsJdwpConfigured()) {
536376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    // No JDWP for you!
537376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    return;
538376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  }
539376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes
540719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  CHECK(gRegistry == nullptr);
541475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gRegistry = new ObjectRegistry;
542475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
543719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  alloc_tracker_lock_ = new Mutex("AllocTracker lock");
5444d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  deoptimization_lock_ = new Mutex("deoptimization lock", kDeoptimizationLock);
545d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // Init JDWP if the debugger is enabled. This may connect out to a
546d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger, passively listen for a debugger, or block waiting for a
547d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger.
548376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
549376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState == NULL) {
550f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // We probably failed because some other process has the port already, which means that
551f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // if we don't abort the user is likely to think they're talking to us when they're actually
552f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // talking to that other process.
5533d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Debugger thread failed to initialize";
554d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
555d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes
556d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // If a debugger has already attached, send the "welcome" message.
557d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // This may cause us to suspend all threads.
558376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState->IsActive()) {
55900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
560376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    if (!gJdwpState->PostVMStart()) {
5613d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes      LOG(WARNING) << "Failed to post 'start' message to debugger";
562d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes    }
563d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
564872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
565872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
5663b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid Dbg::VisitRoots(RootCallback* callback, void* arg) {
5674d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  {
5684d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
5694d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    for (Breakpoint& bp : gBreakpoints) {
5704d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      bp.VisitRoots(callback, arg);
5714d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
5724d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
5734d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  if (deoptimization_lock_ != nullptr) {  // only true if the debugger is started.
5744d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(Thread::Current(), *deoptimization_lock_);
5754d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    for (DeoptimizationRequest& req : deoptimization_requests_) {
5764d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      req.VisitRoots(callback, arg);
5774d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
5783b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
5793b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
5803b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
581d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StopJdwp() {
5820376e6b62bfb43b515b791f0a7afed2d0d8030bcSebastien Hertz  // Prevent the JDWP thread from processing JDWP incoming packets after we close the connection.
5830376e6b62bfb43b515b791f0a7afed2d0d8030bcSebastien Hertz  Disposed();
584376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  delete gJdwpState;
585719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  gJdwpState = nullptr;
586475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  delete gRegistry;
587719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  gRegistry = nullptr;
588719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  delete alloc_tracker_lock_;
589719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  alloc_tracker_lock_ = nullptr;
5904d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  delete deoptimization_lock_;
5914d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  deoptimization_lock_ = nullptr;
592872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
593872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
594767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesvoid Dbg::GcDidFinish() {
595767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
59600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
59781ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes    LOG(DEBUG) << "Sending heap info to DDM";
5987162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(gDdmHpifWhen);
599767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
600767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
60100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
60281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes    LOG(DEBUG) << "Dumping heap to DDM";
6036a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(false);
604767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
605767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
60600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
607767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(DEBUG) << "Dumping native heap to DDM";
6086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(true);
609767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
610767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
611767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
6124ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesvoid Dbg::SetJdwpAllowed(bool allowed) {
6134ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes  gJdwpAllowed = allowed;
6144ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes}
6154ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
616872d4ec7225444d9400d30f9027247deb91012fdElliott HughesDebugInvokeReq* Dbg::GetInvokeReq() {
617475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return Thread::Current()->GetInvokeReq();
618475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
619475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
620475fc23a4a7f35d1be87ea0b06c80df317a720acElliott HughesThread* Dbg::GetDebugThread() {
621475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
622475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
623475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
624475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::ClearWaitForEventThread() {
625475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gJdwpState->ClearWaitForEventThread();
626872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
627872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
628872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Connected() {
6293bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(!gDebuggerConnected);
6304dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "JDWP has attached";
6313bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gDebuggerConnected = true;
6328696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = false;
6338696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
6348696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
6358696433d1b3d8ba15288483b777edd888de69135Elliott Hughesvoid Dbg::Disposed() {
6368696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = true;
6378696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
6388696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
6398696433d1b3d8ba15288483b777edd888de69135Elliott Hughesbool Dbg::IsDisposed() {
6408696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return gDisposed;
641872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
642872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
6433f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz// All the instrumentation events the debugger is registered for.
6443f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzstatic constexpr uint32_t kListenerEvents = instrumentation::Instrumentation::kMethodEntered |
6453f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                            instrumentation::Instrumentation::kMethodExited |
6463f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                            instrumentation::Instrumentation::kDexPcMoved |
6473f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                            instrumentation::Instrumentation::kFieldRead |
6483f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                            instrumentation::Instrumentation::kFieldWritten |
6493f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                            instrumentation::Instrumentation::kExceptionCaught;
6503f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
651a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::GoActive() {
652a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Enable all debugging features, including scans for breakpoints.
653a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // This is a no-op if we're already active.
654a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Only called from the JDWP handler thread.
655a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (gDebuggerActive) {
656a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return;
657a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
658a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
659c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  {
660c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected?
66109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
662c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    CHECK_EQ(gBreakpoints.size(), 0U);
663c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  }
664a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
665138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  {
6664d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(Thread::Current(), *deoptimization_lock_);
6674d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    CHECK_EQ(deoptimization_requests_.size(), 0U);
6684d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    CHECK_EQ(full_deoptimization_event_count_, 0U);
6697ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    CHECK_EQ(delayed_full_undeoptimization_count_, 0U);
670138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
671138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
67262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
67362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
67462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
67562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
67662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_NE(old_state, kRunnable);
677138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetInstrumentation()->EnableDeoptimization();
6783f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  runtime->GetInstrumentation()->AddListener(&gDebugInstrumentationListener, kListenerEvents);
679a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  gDebuggerActive = true;
68062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
68162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
68262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
68362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  LOG(INFO) << "Debugger is active";
684872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
685872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
686872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Disconnected() {
687234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  CHECK(gDebuggerConnected);
688234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
689c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  LOG(INFO) << "Debugger is no longer active";
690234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
69162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread
69262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener
69362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // and clear the object registry.
69462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
69562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
69662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
69762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
698aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz
699aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  // Debugger may not be active at this point.
700aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  if (gDebuggerActive) {
701aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    {
702aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // Since we're going to disable deoptimization, we clear the deoptimization requests queue.
703aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // This prevents us from having any pending deoptimization request when the debugger attaches
704aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // to us again while no event has been requested yet.
7054d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      MutexLock mu(Thread::Current(), *deoptimization_lock_);
7064d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.clear();
7074d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      full_deoptimization_event_count_ = 0U;
7087ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      delayed_full_undeoptimization_count_ = 0U;
709aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    }
7103f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener, kListenerEvents);
711aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    runtime->GetInstrumentation()->DisableDeoptimization();
712aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    gDebuggerActive = false;
713aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  }
714234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gRegistry->Clear();
715234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gDebuggerConnected = false;
71662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
71762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
718872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
719872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
720c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsDebuggerActive() {
7213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gDebuggerActive;
722872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
723872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
724c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsJdwpConfigured() {
7253bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gJdwpConfigured;
726872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
727872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
728872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesint64_t Dbg::LastDebuggerActivity() {
729ca9515205010099d006ac2fac244348a1e673dcbElliott Hughes  return gJdwpState->LastDebuggerActivity();
730872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
731872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
732872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::UndoDebuggerSuspensions() {
733234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
734872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
735872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
73688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetClassName(JDWP::RefTypeId class_id) {
7372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id);
7383d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (o == NULL) {
739436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return "NULL";
7407b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
74164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
74288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id));
7433d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
7443d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (!o->IsClass()) {
7457934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    return StringPrintf("non-class %p", o);  // This is only used for debugging output anyway.
7463d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
747436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return DescriptorToName(ClassHelper(o->AsClass()).GetDescriptor());
7483d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes}
7493d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
75088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) {
751436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
7522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
753436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
754436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
7552435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
75688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  class_object_id = gRegistry->Add(c);
757436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
7588696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
7598696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
76088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) {
7613d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
7622dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
7633d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c == NULL) {
7643d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
7653d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
7663d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c->IsInterface()) {
7673d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    // http://code.google.com/p/android/issues/detail?id=20856
76888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = 0;
7693d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else {
77088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = gRegistry->Add(c->GetSuperClass());
7713d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
7723d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
773872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
774872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
775436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
7762dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
77764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
778436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
779436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
780436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader()));
781436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
782872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
783872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
784436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
785436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
7862dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
787436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
788436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
7897b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
790436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
791436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask;
792436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
793de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set,
794de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  // not interfaces.
795436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
796de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  if ((access_flags & kAccInterface) == 0) {
797de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban    access_flags |= kAccSuper;
798de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  }
799436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
800436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAdd4BE(pReply, access_flags);
801436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
802436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
803872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
804872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
805f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott HughesJDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply)
806f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
8072dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
80864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
809f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
810f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
811f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
812f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  // Ensure all threads are suspended while we read objects' lock words.
813f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Thread* self = Thread::Current();
8145426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  CHECK_EQ(self->GetState(), kRunnable);
8155426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  self->TransitionFromRunnableToSuspended(kSuspended);
8165426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  Runtime::Current()->GetThreadList()->SuspendAll();
817f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
818f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  MonitorInfo monitor_info(o);
819f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
8205426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  Runtime::Current()->GetThreadList()->ResumeAll();
8215426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  self->TransitionFromSuspendedToRunnable();
822f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
823d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (monitor_info.owner_ != NULL) {
824d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer()));
825f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else {
826f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    expandBufAddObjectId(reply, gRegistry->Add(NULL));
827f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
828d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.entry_count_);
829d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.waiters_.size());
830d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) {
831d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer()));
832f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
833f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  return JDWP::ERR_NONE;
834f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes}
835f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
836734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott HughesJDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id,
837734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes                                      std::vector<JDWP::ObjectId>& monitors,
83852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                      std::vector<uint32_t>& stack_depths) {
8394993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
8404993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
8414993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  Thread* thread;
8424993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
8434993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  if (error != JDWP::ERR_NONE) {
8444993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return error;
8454993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
8464993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
8474993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
8484993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
8494993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8504993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  struct OwnedMonitorVisitor : public StackVisitor {
8517a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    OwnedMonitorVisitor(Thread* thread, Context* context)
8524993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
8537a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context), current_stack_depth(0) {}
8544993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8554993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
8564993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // annotalysis.
8574993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
8584993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      if (!GetMethod()->IsRuntimeMethod()) {
8594993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        Monitor::VisitLocks(this, AppendOwnedMonitors, this);
860734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes        ++current_stack_depth;
8614993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      }
8624993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      return true;
8634993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
8644993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8652dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) {
8667a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg);
867734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes      visitor->monitors.push_back(owned_monitor);
868734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes      visitor->stack_depths.push_back(visitor->current_stack_depth);
8694993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
8704993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
871734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    size_t current_stack_depth;
8722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    std::vector<mirror::Object*> monitors;
873734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    std::vector<uint32_t> stack_depths;
8744993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  };
8757a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  UniquePtr<Context> context(Context::Create());
8767a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  OwnedMonitorVisitor visitor(thread, context.get());
8774993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  visitor.WalkStack();
8784993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8794993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  for (size_t i = 0; i < visitor.monitors.size(); ++i) {
8804993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    monitors.push_back(gRegistry->Add(visitor.monitors[i]));
881734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    stack_depths.push_back(visitor.stack_depths[i]);
8824993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
8834993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8844993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  return JDWP::ERR_NONE;
8854993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes}
8864993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
88752d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien HertzJDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id,
88852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                         JDWP::ObjectId& contended_monitor) {
889f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
890f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
891f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
892f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
893f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
894f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
895f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
896f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
897f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
898f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
899f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
900f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  contended_monitor = gRegistry->Add(Monitor::GetContendedMonitor(thread));
901f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
902f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
903f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
904f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
905ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott HughesJDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids,
906ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes                                       std::vector<uint64_t>& counts)
907ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
908412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
909412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
9102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
911ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  counts.clear();
912ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  for (size_t i = 0; i < class_ids.size(); ++i) {
913ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    JDWP::JdwpError status;
9142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_ids[i], status);
915ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    if (c == NULL) {
916ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes      return status;
917ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    }
918ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    classes.push_back(c);
919ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    counts.push_back(0);
920ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  }
921412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CountInstances(classes, false, &counts[0]);
922ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  return JDWP::ERR_NONE;
923ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes}
924ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes
9253b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott HughesJDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, std::vector<JDWP::ObjectId>& instances)
9263b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
927412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
928412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  // We only want reachable instances, so do a GC.
929412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
9303b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  JDWP::JdwpError status;
9312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
932412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (c == nullptr) {
9333b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    return status;
9343b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
9352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
9363b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances);
9373b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
9383b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    instances.push_back(gRegistry->Add(raw_instances[i]));
9393b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
9403b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  return JDWP::ERR_NONE;
9413b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes}
9423b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes
9430cbaff584244ee767027aff35cd3c625aaee2994Elliott HughesJDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count,
9440cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes                                         std::vector<JDWP::ObjectId>& referring_objects)
9450cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
946412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
947412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
9482dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
94964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
9500cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
9510cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
9522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
953412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->GetReferringObjects(o, max_count, raw_instances);
9540cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
9550cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    referring_objects.push_back(gRegistry->Add(raw_instances[i]));
9560cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
9570cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  return JDWP::ERR_NONE;
9580cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes}
9590cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes
96064f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id)
96164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
962e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
963e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
964e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
965e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
96664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisableCollection(object_id);
96764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
96864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
96964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
97064f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id)
97164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
972e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
973e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI
974e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // also ignores these cases and never return an error. However it's not obvious why this command
975e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // should behave differently from DisableCollection and IsCollected commands. So let's be more
976e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // strict and return an error if this happens.
977e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
978e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
979e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
98064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->EnableCollection(object_id);
98164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
98264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
98364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
98464f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected)
98564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
98665637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  if (object_id == 0) {
98765637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    // Null object id is invalid.
98865637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
98965637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  }
990e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However
99165637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  // the RI seems to ignore this and assume object has been collected.
99265637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
993e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
99465637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    is_collected = true;
99565637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  } else {
99665637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    is_collected = gRegistry->IsCollected(object_id);
997e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
99864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
99964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
100064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
100164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count)
100264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
100364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisposeObject(object_id, reference_count);
100464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
100564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
10064d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertzstatic JDWP::JdwpTypeTag GetTypeTag(mirror::Class* klass)
10074d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
10084d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  DCHECK(klass != nullptr);
10094d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  if (klass->IsArrayClass()) {
10104d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_ARRAY;
10114d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  } else if (klass->IsInterface()) {
10124d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_INTERFACE;
10134d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  } else {
10144d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_CLASS;
10154d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  }
10164d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz}
10174d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz
101888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1019436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
10202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1021436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1022436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
10237b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1024436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
10254d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag type_tag = GetTypeTag(c);
10264d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  expandBufAdd1(pReply, type_tag);
102788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  expandBufAddRefTypeId(pReply, class_id);
1028436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1029872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1030872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
10317b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughesvoid Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) {
1032a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Get the complete list of reference classes (i.e. all classes except
1033a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // the primitive types).
1034a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Returns a newly-allocated buffer full of RefTypeId values.
1035a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  struct ClassListCreator {
1036ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) {
10377b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    }
10387b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
10392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static bool Visit(mirror::Class* c, void* arg) {
1040a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return reinterpret_cast<ClassListCreator*>(arg)->Visit(c);
1041a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1042a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
104364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
104464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
104564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS {
1046a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (!c->IsPrimitive()) {
104764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes        classes.push_back(gRegistry->AddRefType(c));
1048a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
1049a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return true;
1050a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1051a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
10527b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    std::vector<JDWP::RefTypeId>& classes;
1053a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
1054a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
10557b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  ClassListCreator clc(classes);
1056a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc);
1057872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1058872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
105988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) {
1060436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
10612dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1062436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1063436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
10647b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
10657b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
1066a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (c->IsArrayClass()) {
1067a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
1068a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = JDWP::TT_ARRAY;
1069a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
1070a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsErroneous()) {
1071a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_ERROR;
1072a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    } else {
1073a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED;
1074a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1075a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
1076a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
1077a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
1078a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (pDescriptor != NULL) {
1079dfb325e0ddd746cd8f7c2e3723b3a573eb7cc111Ian Rogers    *pDescriptor = ClassHelper(c).GetDescriptor();
1080a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
1081436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1082872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1083872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1084c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughesvoid Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) {
10852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
10866fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes);
10876fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  ids.clear();
10886fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  for (size_t i = 0; i < classes.size(); ++i) {
10896fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes    ids.push_back(gRegistry->Add(classes[i]));
10906fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  }
1091872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1092872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
109364f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply)
109464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
10952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
109664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
10972435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
10982435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
10992435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
11004d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass());
110164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass());
11022435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
11032435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAdd1(pReply, type_tag);
11042435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAddRefTypeId(pReply, type_id);
11052435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
11062435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1107872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1108872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1109fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan RogersJDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) {
11101fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  JDWP::JdwpError status;
11112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
11121fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  if (c == NULL) {
11131fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes    return status;
11147b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1115dfb325e0ddd746cd8f7c2e3723b3a573eb7cc111Ian Rogers  *signature = ClassHelper(c).GetDescriptor();
11161fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  return JDWP::ERR_NONE;
1117872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1118872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
111988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result) {
1120436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1122436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1123436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
11247b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1125b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz  if (c->IsProxyClass()) {
1126b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz    return JDWP::ERR_ABSENT_INFORMATION;
1127b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz  }
1128436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  result = ClassHelper(c).GetSourceFile();
1129436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1130872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1131872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
113288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) {
11339837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
11342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
113564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
1136546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
1137546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
11389837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  tag = TagFromObject(soa, o);
1139546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  return JDWP::ERR_NONE;
1140872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1141872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1142aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughessize_t Dbg::GetTagWidth(JDWP::JdwpTag tag) {
1143dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  switch (tag) {
1144dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_VOID:
1145dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 0;
1146dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BYTE:
1147dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BOOLEAN:
1148dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 1;
1149dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CHAR:
1150dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_SHORT:
1151dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 2;
1152dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_FLOAT:
1153dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_INT:
1154dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 4;
1155dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_ARRAY:
1156dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_OBJECT:
1157dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_STRING:
1158dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD:
1159dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD_GROUP:
1160dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_LOADER:
1161dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_OBJECT:
1162dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return sizeof(JDWP::ObjectId);
1163dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_DOUBLE:
1164dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_LONG:
1165dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 8;
1166dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  default:
11673d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Unknown tag " << tag;
1168dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return -1;
1169dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1170872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1171872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
117288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int& length) {
11733d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
11742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
11753d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (a == NULL) {
11763d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
117724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
11783d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  length = a->GetLength();
11793d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1180872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1181872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
118288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) {
11833d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
11842dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
11859837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  if (a == nullptr) {
11863d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
11873d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
118824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
118924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) {
119024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
11913d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
119224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
11936d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor());
119424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
119524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
11963d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd1(pReply, tag);
11973d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd4BE(pReply, count);
11983d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
119924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (IsPrimitiveTag(tag)) {
120024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    size_t width = GetTagWidth(tag);
120124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    uint8_t* dst = expandBufAddSpace(pReply, count * width);
120224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    if (width == 8) {
1203ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t), 0));
120424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]);
120524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 4) {
1206ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t), 0));
120724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]);
120824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 2) {
1209ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t), 0));
121024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]);
121124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else {
1212ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t), 0));
121324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      memcpy(dst, &src[offset * width], count * width);
121424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
121524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  } else {
12169837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
12172dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>();
121824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    for (int i = 0; i < count; ++i) {
12192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* element = oa->Get(offset + i);
12209837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element)
12219837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers                                                        : tag;
122224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAdd1(pReply, specific_tag);
122324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAddObjectId(pReply, gRegistry->Add(element));
122424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
122524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
122624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
12273d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1228872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1229872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1230ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogerstemplate <typename T>
1231ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstatic void CopyArrayData(mirror::Array* a, JDWP::Request& src, int offset, int count)
1232ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    NO_THREAD_SAFETY_ANALYSIS {
1233ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  // TODO: fix when annotalysis correctly handles non-member functions.
12344b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  DCHECK(a->GetClass()->IsPrimitiveArray());
12354b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1236ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset));
12374b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  for (int i = 0; i < count; ++i) {
12384b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    *dst++ = src.ReadValue(sizeof(T));
12394b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  }
12404b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
12414b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
124288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count,
12434b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                      JDWP::Request& request)
1244b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
12453d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
12464b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  mirror::Array* dst = DecodeArray(array_id, status);
12474b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (dst == NULL) {
12483d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
12493d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
1250f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
12514b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) {
1252f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
12533d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
1254f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
12551d66e88ea74f7d85f75db7f145fe955d5f0f6872nikolay serdjuk  ClassHelper ch(dst->GetClass());
12561d66e88ea74f7d85f75db7f145fe955d5f0f6872nikolay serdjuk  const char* descriptor = ch.GetDescriptor();
1257fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor + 1);
1258f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
1259f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  if (IsPrimitiveTag(tag)) {
1260f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    size_t width = GetTagWidth(tag);
1261f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    if (width == 8) {
12624b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint64_t>(dst, request, offset, count);
1263f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 4) {
12644b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint32_t>(dst, request, offset, count);
1265f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 2) {
12664b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint16_t>(dst, request, offset, count);
1267f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else {
12684b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint8_t>(dst, request, offset, count);
1269f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1270f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  } else {
12714b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>();
1272f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    for (int i = 0; i < count; ++i) {
12734b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      JDWP::ObjectId id = request.ReadObjectId();
12742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
127564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes      if (o == ObjectRegistry::kInvalidObject) {
1276436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
1277436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes      }
1278d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      oa->Set<false>(offset + i, o);
1279f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1280f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1281f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
12823d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1283872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1284872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
12857b3cdfcca472b779cf8745fb8460935e56229f11Elliott HughesJDWP::ObjectId Dbg::CreateString(const std::string& str) {
12862dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  return gRegistry->Add(mirror::String::AllocFromModifiedUtf8(Thread::Current(), str.c_str()));
1287872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1288872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
128988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) {
1290436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
12912dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1292436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1293436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
12947b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
129550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  new_object = gRegistry->Add(c->AllocObject(Thread::Current()));
1296436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1297872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1298872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1299bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes/*
1300bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".
1301bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes */
130288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length,
130300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                       JDWP::ObjectId& new_array) {
1304436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
13052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(array_class_id, status);
1306436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1307436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
13087b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
13096fac447555dc94a935b78198479cce645c837b89Ian Rogers  new_array = gRegistry->Add(mirror::Array::Alloc<true>(Thread::Current(), c, length,
13106fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                        c->GetComponentSize(),
13116fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                        Runtime::Current()->GetHeap()->GetCurrentAllocator()));
1312436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1313872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1314872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
131588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesbool Dbg::MatchType(JDWP::RefTypeId instance_class_id, JDWP::RefTypeId class_id) {
1316436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
13172dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c1 = DecodeClass(instance_class_id, status);
1318a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  CHECK(c1 != NULL);
13192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c2 = DecodeClass(class_id, status);
1320a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  CHECK(c2 != NULL);
1321123756a041baf8421ed933312605daa5ef082f6fSebastien Hertz  return c2->IsAssignableFrom(c1);
1322872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1323872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1324ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::FieldId ToFieldId(const mirror::ArtField* f)
1325b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1326590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
132703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f));
132803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
132903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1330ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::MethodId ToMethodId(const mirror::ArtMethod* m)
1331b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1332590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
133303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m));
133403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
133503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1336ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtField* FromFieldId(JDWP::FieldId fid)
1337b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1338590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
1339ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtField*>(static_cast<uintptr_t>(fid));
1340aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes}
1341aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1342ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtMethod* FromMethodId(JDWP::MethodId mid)
1343b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1344590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
1345ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtMethod*>(static_cast<uintptr_t>(mid));
134603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
134703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1348ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic void SetLocation(JDWP::JdwpLocation& location, mirror::ArtMethod* m, uint32_t dex_pc)
1349b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
135091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (m == NULL) {
135191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    memset(&location, 0, sizeof(location));
135291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  } else {
13532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = m->GetDeclaringClass();
13544d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    location.type_tag = GetTypeTag(c);
1355cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    location.class_id = gRegistry->AddRefType(c);
1356748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    location.method_id = ToMethodId(m);
1357cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    location.dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint64_t>(-1) : dex_pc;
135891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
1359d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes}
1360d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
1361a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetMethodName(JDWP::MethodId method_id)
1362b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1363ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
13646d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  return MethodHelper(m).GetName();
1365872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1366872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1367a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetFieldName(JDWP::FieldId field_id)
1368a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1369ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
1370a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes  return FieldHelper(f).GetName();
1371a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes}
1372a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes
1373a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes/*
1374a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * Augment the access flags for synthetic methods and fields by setting
1375a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * the (as described by the spec) "0xf0000000 bit".  Also, strip out any
1376a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * flags not specified by the Java programming language.
1377a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes */
1378a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughesstatic uint32_t MangleAccessFlags(uint32_t accessFlags) {
1379a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  accessFlags &= kAccJavaFlagsMask;
1380a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  if ((accessFlags & kAccSynthetic) != 0) {
1381a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    accessFlags |= 0xf0000000;
1382a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1383a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return accessFlags;
1384872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1385872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1386dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes/*
1387b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come first. Debuggers
1388b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * expect slots to begin with arguments, but dex code places them at
1389b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * the end.
1390dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes */
1391b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Haostatic uint16_t MangleSlot(uint16_t slot, mirror::ArtMethod* m)
1392b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1393b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
1394cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1395cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // We should not get here for a method without code (native, proxy or abstract). Log it and
1396cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // return the slot as is since all registers are arguments.
1397cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    LOG(WARNING) << "Trying to mangle slot for method without code " << PrettyMethod(m);
1398cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return slot;
1399cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1400b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1401b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1402b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot >= locals_size) {
1403b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - locals_size;
1404b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1405b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + ins_size;
1406dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1407dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1408dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1409b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao/*
1410b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come last. Reverts
1411b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * slots to dex style argument placement.
1412b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao */
1413ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic uint16_t DemangleSlot(uint16_t slot, mirror::ArtMethod* m)
1414b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1415b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
1416cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1417cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // We should not get here for a method without code (native, proxy or abstract). Log it and
1418cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // return the slot as is since all registers are arguments.
1419cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    LOG(WARNING) << "Trying to demangle slot for method without code " << PrettyMethod(m);
1420cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return slot;
1421cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1422b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1423b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1424b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot < ins_size) {
1425b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + locals_size;
1426b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1427b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - ins_size;
1428dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1429dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1430dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
143188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1432436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1434436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1435436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
14367b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1437a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1438a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t instance_field_count = c->NumInstanceFields();
1439a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t static_field_count = c->NumStaticFields();
1440a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1441a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, instance_field_count + static_field_count);
1442a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1443a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < instance_field_count + static_field_count; ++i) {
1444ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count);
14456d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    FieldHelper fh(f);
1446a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddFieldId(pReply, ToFieldId(f));
14476d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, fh.GetName());
14486d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, fh.GetTypeDescriptor());
1449c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1450a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1451a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1452a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1453a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags()));
1454a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1455436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1456a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1457a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
145888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic,
145900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                           JDWP::ExpandBuf* pReply) {
1460436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14612dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1462436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1463436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
14647b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1465a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1466a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t direct_method_count = c->NumDirectMethods();
1467a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t virtual_method_count = c->NumVirtualMethods();
1468a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1469a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, direct_method_count + virtual_method_count);
1470a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1471a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) {
1472ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
14736d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    MethodHelper mh(m);
1474a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddMethodId(pReply, ToMethodId(m));
14756d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, mh.GetName());
1476d91d6d6a80748f277fd938a412211e5af28913b1Ian Rogers    expandBufAddUtf8String(pReply, mh.GetSignature().ToString());
1477c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1478a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1479a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1480a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1481a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags()));
1482a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1483436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1484a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1485a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
148688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1487436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14882dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1489436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1490436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
14917b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1492436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
1493436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  ClassHelper kh(c);
1494d24e264ff85ad8c6f142ac6d33055fdc1881fa2fIan Rogers  size_t interface_count = kh.NumDirectInterfaces();
1495a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, interface_count);
1496a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < interface_count; ++i) {
149764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    expandBufAddRefTypeId(pReply, gRegistry->AddRefType(kh.GetDirectInterface(i)));
1498a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1499436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1500872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1501872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
150288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply)
1503b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
150403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  struct DebugCallbackContext {
150503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    int numItems;
150603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    JDWP::ExpandBuf* pReply;
150703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
15082435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    static bool Callback(void* context, uint32_t address, uint32_t line_number) {
150903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
151003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      expandBufAdd8BE(pContext->pReply, address);
15112435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      expandBufAdd4BE(pContext->pReply, line_number);
151203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      pContext->numItems++;
1513f2910eef247b45ce1d489e323b36b5de6b6157aaSebastien Hertz      return false;
151403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
151503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
1516ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
15176d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  MethodHelper mh(m);
1518cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  const DexFile::CodeItem* code_item = mh.GetCodeItem();
151903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  uint64_t start, end;
1520cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1521cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    DCHECK(m->IsNative() || m->IsProxyMethod());
152203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = -1;
152303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    end = -1;
152403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  } else {
152503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = 0;
152614f0db92225d34622fa5cb1a6dc9287334aaf6c7jeffhao    // Return the index of the last instruction
1527cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    end = code_item->insns_size_in_code_units_ - 1;
152803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  }
152903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
153003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, start);
153103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, end);
153203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
153303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  // Add numLines later
153403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  size_t numLinesOffset = expandBufGetLength(pReply);
153503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd4BE(pReply, 0);
153603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
153703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  DebugCallbackContext context;
153803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.numItems = 0;
153903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.pReply = pReply;
154003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1541cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item != nullptr) {
1542cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
1543cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                    DebugCallbackContext::Callback, NULL, &context);
1544cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
154503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
154603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems);
1547872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1548872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
154988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1550dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  struct DebugCallbackContext {
1551b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    mirror::ArtMethod* method;
1552dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    JDWP::ExpandBuf* pReply;
1553c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    size_t variable_count;
1554c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    bool with_generic;
1555dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1556b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, const char* name, const char* descriptor, const char* signature)
1557b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1558dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1559dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1560b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff 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));
1561dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1562b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao      slot = MangleSlot(slot, pContext->method);
156368fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
1564dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd8BE(pContext->pReply, startAddress);
1565dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, name);
1566dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, descriptor);
1567c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      if (pContext->with_generic) {
1568dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes        expandBufAddUtf8String(pContext->pReply, signature);
1569dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      }
1570dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, endAddress - startAddress);
1571dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, slot);
1572dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1573c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      ++pContext->variable_count;
1574dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    }
1575dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  };
1576ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
15776d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  MethodHelper mh(m);
1578dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1579c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // arg_count considers doubles and longs to take 2 units.
1580c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // variable_count considers everything to take 1 unit.
15816d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  std::string shorty(mh.GetShorty());
1582ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  expandBufAdd4BE(pReply, mirror::ArtMethod::NumArgRegisters(shorty));
1583dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1584c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // We don't know the total number of variables yet, so leave a blank and update it later.
1585c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  size_t variable_count_offset = expandBufGetLength(pReply);
1586dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  expandBufAdd4BE(pReply, 0);
1587dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1588dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  DebugCallbackContext context;
1589b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  context.method = m;
1590dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  context.pReply = pReply;
1591c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.variable_count = 0;
1592c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.with_generic = with_generic;
1593dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1594cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  const DexFile::CodeItem* code_item = mh.GetCodeItem();
1595cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item != nullptr) {
1596cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL,
1597cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                    DebugCallbackContext::Callback, &context);
1598cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1599dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1600c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count);
1601872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1602872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1603579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value,
1604579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                                  JDWP::ExpandBuf* pReply) {
1605579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  mirror::ArtMethod* m = FromMethodId(method_id);
1606579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  JDWP::JdwpTag tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty());
1607579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  OutputJValue(tag, return_value, pReply);
1608579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1609579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
16103f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value,
16113f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                           JDWP::ExpandBuf* pReply) {
16123f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  mirror::ArtField* f = FromFieldId(field_id);
16133f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
16143f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  OutputJValue(tag, field_value, pReply);
16153f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
16163f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
16179777ba230c83a0edcbda2cf7b208339e77bf171bElliott HughesJDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id,
16189777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes                                  std::vector<uint8_t>& bytecodes)
16199777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1620ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
16219777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  if (m == NULL) {
16229777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    return JDWP::ERR_INVALID_METHODID;
16239777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
16249777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  MethodHelper mh(m);
16259777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const DexFile::CodeItem* code_item = mh.GetCodeItem();
16269777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  size_t byte_count = code_item->insns_size_in_code_units_ * 2;
16279777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_);
16289777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* end = begin + byte_count;
16299777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  for (const uint8_t* p = begin; p != end; ++p) {
16309777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    bytecodes.push_back(*p);
16319777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
16329777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  return JDWP::ERR_NONE;
16339777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes}
16349777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes
163588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) {
163688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
1637872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1638872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
163988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) {
164088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
1641872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1642872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
164388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id,
164488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                         JDWP::FieldId field_id, JDWP::ExpandBuf* pReply,
164500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         bool is_static)
1646b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16470cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  JDWP::JdwpError status;
16482dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(ref_type_id, status);
164988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  if (ref_type_id != 0 && c == NULL) {
16500cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    return status;
16510cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
16520cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
16532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
165464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
16553f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
16563f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1657ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
16580cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
16592dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* receiver_class = c;
16600cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class == NULL && o != NULL) {
16610cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    receiver_class = o->GetClass();
16620cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
16630cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we give up now if receiver_class is NULL?
16640cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) {
16650cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class);
16663f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_FIELDID;
16673f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1668aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
16690cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
16700cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
16710cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
16720cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
16730cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
16740cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
16750cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
16760cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
16770cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
16780cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
16790cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
16800dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
16810dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
16820dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
16830cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
16846d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
1685579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  JValue field_value;
1686579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (tag == JDWP::JT_VOID) {
1687579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    LOG(FATAL) << "Unknown tag: " << tag;
1688579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (!IsPrimitiveTag(tag)) {
1689579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetL(f->GetObject(o));
1690579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1691579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetJ(f->Get64(o));
1692aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
1693579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetI(f->Get32(o));
1694aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
1695579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  Dbg::OutputJValue(tag, &field_value, pReply);
1696579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
16973f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  return JDWP::ERR_NONE;
1698872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1699872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
170088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id,
170100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ExpandBuf* pReply) {
170288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(0, object_id, field_id, pReply, false);
17033f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
17043f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
170588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, JDWP::ExpandBuf* pReply) {
170688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true);
17073f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
17083f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
170988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id,
171000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         uint64_t value, int width, bool is_static)
1711b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
17122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
171364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
17143f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
17153f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1716ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
17170cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
17180cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
17190cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
17200cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
17210cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
17220cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
17230cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
17240cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
17250cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
17260cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
17270cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
17283f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
17290dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
17300dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
17310dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
1732aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
17336d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
1734aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1735aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  if (IsPrimitiveTag(tag)) {
1736aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
17371bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_EQ(width, 8);
1738d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      // Debugging can't use transactional mode (runtime only).
1739d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      f->Set64<false>(o, value);
1740aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    } else {
17411bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_LE(width, 4);
1742d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      // Debugging can't use transactional mode (runtime only).
1743d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      f->Set32<false>(o, value);
1744aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    }
1745aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
17462dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* v = gRegistry->Get<mirror::Object*>(value);
174764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (v == ObjectRegistry::kInvalidObject) {
17483d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes      return JDWP::ERR_INVALID_OBJECT;
17493d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    }
17503f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    if (v != NULL) {
17512dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* field_type = FieldHelper(f).GetType();
17523f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      if (!field_type->IsAssignableFrom(v->GetClass())) {
17533f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
17543f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      }
17553f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
1756d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    // Debugging can't use transactional mode (runtime only).
1757d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    f->SetObject<false>(o, v);
1758aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
17593d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
17603d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1761872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1762872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
176388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value,
176400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   int width) {
176588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(object_id, field_id, value, width, false);
1766872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1767872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
176888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) {
176988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(0, field_id, value, width, true);
1770872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1771872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
177288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::StringToUtf8(JDWP::ObjectId string_id) {
17732dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = gRegistry->Get<mirror::String*>(string_id);
177468fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  return s->ToModifiedUtf8();
1775872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1776872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1777579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) {
1778579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (IsPrimitiveTag(tag)) {
1779579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAdd1(pReply, tag);
1780579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) {
1781579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd1(pReply, return_value->GetI());
1782579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) {
1783579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd2BE(pReply, return_value->GetI());
1784579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) {
1785579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd4BE(pReply, return_value->GetI());
1786579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1787579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd8BE(pReply, return_value->GetJ());
1788579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else {
1789579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      CHECK_EQ(tag, JDWP::JT_VOID);
1790579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    }
1791579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else {
17929837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
1793579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    mirror::Object* value = return_value->GetL();
17949837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    expandBufAdd1(pReply, TagFromObject(soa, value));
1795579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAddObjectId(pReply, gRegistry->Add(value));
1796579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  }
1797579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1798579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
1799221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string& name) {
1800a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  ScopedObjectAccessUnchecked soa(Thread::Current());
1801a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1802221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1803221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1804221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) {
1805221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1806a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1807221229cb523f849f165fdafbf9785010963715daElliott Hughes
1808221229cb523f849f165fdafbf9785010963715daElliott Hughes  // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName.
18092dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
1810ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* java_lang_Thread_name_field =
18112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      soa.DecodeField(WellKnownClasses::java_lang_Thread_name);
18122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s =
18132dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object));
1814221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (s != NULL) {
1815221229cb523f849f165fdafbf9785010963715daElliott Hughes    name = s->ToModifiedUtf8();
1816221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1817221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1818872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1819872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1820221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
182100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
18222dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
182364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_object == ObjectRegistry::kInvalidObject) {
18242435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
18252435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
18269837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroup");
18272435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Okay, so it's an object, but is it actually a thread?
182850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1829221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1830221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1831221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1832221229cb523f849f165fdafbf9785010963715daElliott Hughes    // Zombie threads are in the null group.
1833221229cb523f849f165fdafbf9785010963715daElliott Hughes    expandBufAddObjectId(pReply, JDWP::ObjectId(0));
183452d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    error = JDWP::ERR_NONE;
183552d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz  } else if (error == JDWP::ERR_NONE) {
183652d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
183752d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    CHECK(c != nullptr);
183852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::ArtField* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;");
183952d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    CHECK(f != NULL);
184052d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::Object* group = f->GetObject(thread_object);
184152d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    CHECK(group != NULL);
184252d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    JDWP::ObjectId thread_group_id = gRegistry->Add(group);
184352d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    expandBufAddObjectId(pReply, thread_group_id);
18442435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
18459837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
184652d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz  return error;
1847872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1848872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
184988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id) {
185000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
18512dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
18529837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(thread_group != nullptr);
18539837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroupName");
18549837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
18559837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(c != nullptr);
1856ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("name", "Ljava/lang/String;");
1857499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(f != NULL);
18582dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group));
18599837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
1860499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  return s->ToModifiedUtf8();
1861872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1862872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
186388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id) {
18649837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
18652dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
18669837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(thread_group != nullptr);
18679837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroupParent");
18689837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
18699837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(c != nullptr);
1870ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;");
18714e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  CHECK(f != NULL);
18722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* parent = f->GetObject(thread_group);
18739837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
18744e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  return gRegistry->Add(parent);
1875872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1876872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1877872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetSystemThreadGroupId() {
187800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
1879ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup);
18802dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
1881365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
1882872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1883872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1884872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetMainThreadGroupId() {
188500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1886ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup);
18872dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
1888365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
1889872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1890872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1891920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff HaoJDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) {
1892920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  switch (state) {
1893920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kBlocked:
1894920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_MONITOR;
1895920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kNative:
1896920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kRunnable:
1897920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSuspended:
1898920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_RUNNING;
1899920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSleeping:
1900920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_SLEEPING;
1901920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kStarting:
1902920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTerminated:
1903920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_ZOMBIE;
1904920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTimedWaiting:
1905920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSend:
1906920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSuspension:
1907920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerToAttach:
1908138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    case kWaitingForDeoptimization:
1909920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForGcToComplete:
1910920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForCheckPointsToRun:
1911920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForJniOnLoad:
1912920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForSignalCatcherOutput:
1913920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainDebuggerLoop:
1914920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainSignalCatcherLoop:
1915920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingPerformingGc:
1916920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaiting:
1917920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_WAIT;
1918920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      // Don't add a 'default' here so the compiler can spot incompatible enum changes.
1919920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  }
1920920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  LOG(FATAL) << "Unknown thread state: " << state;
1921920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  return JDWP::TS_ZOMBIE;
1922920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao}
1923920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao
192452d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien HertzJDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus,
192552d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                     JDWP::JdwpSuspendStatus* pSuspendStatus) {
192600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1927499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
19289e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED;
19299e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
193050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1931221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1932221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1933221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1934221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1935221229cb523f849f165fdafbf9785010963715daElliott Hughes      *pThreadStatus = JDWP::TS_ZOMBIE;
1936221229cb523f849f165fdafbf9785010963715daElliott Hughes      return JDWP::ERR_NONE;
1937221229cb523f849f165fdafbf9785010963715daElliott Hughes    }
1938221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1939499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  }
1940499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
19419e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  if (IsSuspendedForDebugger(soa, thread)) {
19429e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED;
19439e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  }
194400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
1945920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  *pThreadStatus = ToJdwpThreadStatus(thread->GetState());
1946221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1947872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1948872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1949221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
195000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
195150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1952221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1953221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1954221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1955221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
19562435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
195750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
195800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  expandBufAdd4BE(pReply, thread->GetDebugSuspendCount());
19592435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1960872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1961872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1962f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott HughesJDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) {
1963f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccess soa(Thread::Current());
1964f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1965f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
1966f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1967f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
1968f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
1969f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
1970dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  thread->Interrupt(soa.Self());
1971f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
1972f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
1973f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
1974caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughesvoid Dbg::GetThreads(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& thread_ids) {
1975365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  class ThreadListVisitor {
1976365c10235438607541fa2259a5fec48061b90bd8Ian Rogers   public:
19772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    ThreadListVisitor(const ScopedObjectAccessUnchecked& soa, mirror::Object* desired_thread_group,
197800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                      std::vector<JDWP::ObjectId>& thread_ids)
1979b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
19800dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        : soa_(soa), desired_thread_group_(desired_thread_group), thread_ids_(thread_ids) {}
1981365c10235438607541fa2259a5fec48061b90bd8Ian Rogers
1982a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    static void Visit(Thread* t, void* arg) {
1983a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t);
1984a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1985a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
198600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
198700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
198800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void Visit(Thread* t) NO_THREAD_SAFETY_ANALYSIS {
1989a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (t == Dbg::GetDebugThread()) {
1990a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and
1991a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        // query all threads, so it's easier if we just don't tell them about this thread.
1992a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
1993a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
19942dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* peer = t->GetPeer();
19950dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      if (IsInDesiredThreadGroup(peer)) {
1996120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers        thread_ids_.push_back(gRegistry->Add(peer));
1997a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
1998a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1999a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
2000365c10235438607541fa2259a5fec48061b90bd8Ian Rogers   private:
20012dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    bool IsInDesiredThreadGroup(mirror::Object* peer)
20020dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20030dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      // peer might be NULL if the thread is still starting up.
20040dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      if (peer == NULL) {
20050dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // We can't tell the debugger about this thread yet.
20060dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // TODO: if we identified threads to the debugger by their Thread*
20072dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        // rather than their peer's mirror::Object*, we could fix this.
20080dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // Doing so might help us report ZOMBIE threads too.
20090dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        return false;
20100dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      }
2011c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      // Do we want threads from all thread groups?
2012c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      if (desired_thread_group_ == NULL) {
2013c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao        return true;
2014c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      }
20152dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* group = soa_.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(peer);
20160dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      return (group == desired_thread_group_);
20170dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    }
20180dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao
2019dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier    const ScopedObjectAccessUnchecked& soa_;
20202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* const desired_thread_group_;
2021caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    std::vector<JDWP::ObjectId>& thread_ids_;
2022a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
2023a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
202400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
20252dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
202600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ThreadListVisitor tlv(soa, thread_group, thread_ids);
202750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2028f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughes  Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv);
2029a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes}
2030a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
2031caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughesvoid Dbg::GetChildThreadGroups(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& child_thread_group_ids) {
203200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
20332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
2034caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes
2035caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Get the ArrayList<ThreadGroup> "groups" out of this thread group...
2036ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* groups_field = thread_group->GetClass()->FindInstanceField("groups", "Ljava/util/List;");
20372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* groups_array_list = groups_field->GetObject(thread_group);
2038872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2039caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Get the array and size out of the ArrayList<ThreadGroup>...
2040ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* array_field = groups_array_list->GetClass()->FindInstanceField("array", "[Ljava/lang/Object;");
2041ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* size_field = groups_array_list->GetClass()->FindInstanceField("size", "I");
20422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::ObjectArray<mirror::Object>* groups_array =
20432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>();
2044caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  const int32_t size = size_field->GetInt(groups_array_list);
2045caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes
2046caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Copy the first 'size' elements out of the array into the result.
2047caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  for (int32_t i = 0; i < size; ++i) {
2048caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    child_thread_group_ids.push_back(gRegistry->Add(groups_array->Get(i)));
2049caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  }
2050872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2051872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
205200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic int GetStackDepth(Thread* thread)
2053b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct CountStackDepthVisitor : public StackVisitor {
205593ba893c20532990a430741e0a97212900094e8cBrian Carlstrom    explicit CountStackDepthVisitor(Thread* thread)
20567a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth(0) {}
20570399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
205864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
205964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
206064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
20610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!GetMethod()->IsRuntimeMethod()) {
2062f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes        ++depth;
2063f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes      }
2064530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
2065a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
2066a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    size_t depth;
2067a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  };
206808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
20697a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  CountStackDepthVisitor visitor(thread);
20700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2071a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return visitor.depth;
2072872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2073872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2074221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result) {
207500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
2076a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2077221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2078221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2079221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2080221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2081221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2082f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
2083f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
2084f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
2085221229cb523f849f165fdafbf9785010963715daElliott Hughes  result = GetStackDepth(thread);
2086221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
20878696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
20888696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2089306057fd278d75bf3794bd5243a3b6652c487d18Ian RogersJDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame,
2090306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers                                     size_t frame_count, JDWP::ExpandBuf* buf) {
20916e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  class GetFrameVisitor : public StackVisitor {
20926e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   public:
20937a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    GetFrameVisitor(Thread* thread, size_t start_frame, size_t frame_count, JDWP::ExpandBuf* buf)
2094b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
20957a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth_(0),
20966e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes          start_frame_(start_frame), frame_count_(frame_count), buf_(buf) {
20976e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      expandBufAdd4BE(buf_, frame_count_);
209803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
20990399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
210000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
210100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
210200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
21030399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetMethod()->IsRuntimeMethod()) {
21047934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        return true;  // The debugger can't do anything useful with a frame that has no Method*.
210503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
21066e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_ + frame_count_) {
2107530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes        return false;
210803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
21096e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_) {
21106e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::FrameId frame_id(GetFrameId());
21116e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::JdwpLocation location;
21126e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        SetLocation(location, GetMethod(), GetDexPc());
2113ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        VLOG(jdwp) << StringPrintf("    Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location;
21146e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAdd8BE(buf_, frame_id);
21156e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAddLocation(buf_, location);
21166e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      }
21176e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      ++depth_;
2118530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
211903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
21206e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes
21216e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   private:
21226e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    size_t depth_;
21236e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t start_frame_;
21246e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t frame_count_;
21256e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    JDWP::ExpandBuf* buf_;
212603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
212700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
212800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2129a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2130221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2131221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2132221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2133221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2134221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2135f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
2136f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
2137f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
21387a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetFrameVisitor visitor(thread, start_frame, frame_count, buf);
21390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
21406e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
2141872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2142872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2143872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetThreadSelfId() {
2144dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier  ScopedObjectAccessUnchecked soa(Thread::Current());
2145cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  return gRegistry->Add(soa.Self()->GetPeer());
2146872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2147872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2148475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::SuspendVM() {
214900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
2150872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2151872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2152872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ResumeVM() {
2153c61a267e98ed1038b74c33c7740414ced4a27d89Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
2154872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2155872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2156221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) {
215700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedLocalRef<jobject> peer(Thread::Current()->GetJniEnv(), NULL);
215800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
215900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
21602dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id)));
216100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
216200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (peer.get() == NULL) {
216300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
216400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
216500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Suspend thread to build stack trace.
2166f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  bool timed_out;
2167d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  Thread* thread = ThreadList::SuspendThreadByPeer(peer.get(), request_suspension, true,
2168d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                                   &timed_out);
216900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (thread != NULL) {
217000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_NONE;
2171f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else if (timed_out) {
217200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_INTERNAL;
217300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  } else {
217400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
21754e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
2176872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2177872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2178221229cb523f849f165fdafbf9785010963715daElliott Hughesvoid Dbg::ResumeThread(JDWP::ObjectId thread_id) {
217900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
21802dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id);
2181a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  Thread* thread;
2182a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  {
2183a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2184a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    thread = Thread::FromManagedThread(soa, peer);
2185a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  }
21864e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  if (thread == NULL) {
21874e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    LOG(WARNING) << "No such thread for resume: " << peer;
21884e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    return;
21894e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
219000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool needs_resume;
219100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
219250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
219300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    needs_resume = thread->GetSuspendCount() > 0;
219400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
219500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (needs_resume) {
2196546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    Runtime::Current()->GetThreadList()->Resume(thread, true);
2197546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
2198872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2199872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2200872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::SuspendSelf() {
2201475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
2202872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2203872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
22040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct GetThisVisitor : public StackVisitor {
22057a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
2206b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
22077a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, context), this_object(NULL), frame_id(frame_id) {}
220868fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
220900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
221000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
221100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
22126e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    if (frame_id != GetFrameId()) {
22130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return true;  // continue
22140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    } else {
221562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      this_object = GetThisObject();
221662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return false;
22170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
221868fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  }
2219dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
22202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* this_object;
22216e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  JDWP::FrameId frame_id;
22220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers};
2223ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
222400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersJDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id,
222500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ObjectId* result) {
222600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
222700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* thread;
222800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
222950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2230221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2231221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
2232221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
223300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
22349e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    if (!IsSuspendedForDebugger(soa, thread)) {
223500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      return JDWP::ERR_THREAD_NOT_SUSPENDED;
223600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
22376e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  }
2238caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  UniquePtr<Context> context(Context::Create());
22397a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor visitor(thread, context.get(), frame_id);
22400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
22416e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  *result = gRegistry->Add(visitor.this_object);
22426e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
22430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
22440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
2245cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien HertzJDWP::JdwpError Dbg::GetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot,
2246cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                   JDWP::JdwpTag tag, uint8_t* buf, size_t width) {
22470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct GetLocalVisitor : public StackVisitor {
22489837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    GetLocalVisitor(const ScopedObjectAccessUnchecked& soa, Thread* thread, Context* context,
22499837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers                    JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width)
2250b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
22519837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        : StackVisitor(thread, context), soa_(soa), frame_id_(frame_id), slot_(slot), tag_(tag),
2252cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz          buf_(buf), width_(width), error_(JDWP::ERR_NONE) {}
2253ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
225400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
225500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
225600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
22570399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetFrameId() != frame_id_) {
22580399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        return true;  // Not our frame, carry on.
22590399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
22600399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // TODO: check that the tag is compatible with the actual type of the slot!
2261cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      // TODO: check slot is valid for this method or return INVALID_SLOT error.
2262ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* m = GetMethod();
2263cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      if (m->IsNative()) {
2264cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        // We can't read local value from native method.
2265cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        error_ = JDWP::ERR_OPAQUE_FRAME;
2266cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        return false;
2267cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      }
22680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      uint16_t reg = DemangleSlot(slot_, m);
22690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
22700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      switch (tag_) {
22710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_BOOLEAN:
22720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
22730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
22742bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
22750399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get boolean local " << reg << " = " << intVal;
22760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set1(buf_+1, intVal != 0);
22770399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
22780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
22790399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_BYTE:
22800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
22810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
22822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
22830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get byte local " << reg << " = " << intVal;
22840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set1(buf_+1, intVal);
22850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
22860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
22870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_SHORT:
22880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CHAR:
22890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
22900399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 2U);
22912bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
22920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get short/char local " << reg << " = " << intVal;
22930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set2BE(buf_+1, intVal);
22940399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
22950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
22960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_INT:
22972bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        {
22982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 4U);
22992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
23002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          VLOG(jdwp) << "get int local " << reg << " = " << intVal;
23012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          JDWP::Set4BE(buf_+1, intVal);
23022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
23032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        break;
23040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_FLOAT:
23050399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 4U);
23072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kFloatVReg);
23080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get int/float local " << reg << " = " << intVal;
23090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set4BE(buf_+1, intVal);
23100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23120399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_ARRAY:
23130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
23152dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
23160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get array local " << reg << " = " << o;
2317590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier          if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
23180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            LOG(FATAL) << "Register " << reg << " expected to hold array: " << o;
23190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
23200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
23210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CLASS_LOADER:
23240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CLASS_OBJECT:
23250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_OBJECT:
23260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_STRING:
23270399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_THREAD:
23280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_THREAD_GROUP:
23290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
23312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
23320399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get object local " << reg << " = " << o;
2333590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier          if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
23340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            LOG(FATAL) << "Register " << reg << " expected to hold object: " << o;
23350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
23369837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers          tag_ = TagFromObject(soa_, o);
23370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
23380399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_DOUBLE:
23412bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        {
23422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 8U);
23432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t lo = GetVReg(m, reg, kDoubleLoVReg);
23442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t hi = GetVReg(m, reg + 1, kDoubleHiVReg);
23452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t longVal = (hi << 32) | lo;
23462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
23472bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          JDWP::Set8BE(buf_+1, longVal);
23482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
23492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        break;
23500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_LONG:
23510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 8U);
23532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t lo = GetVReg(m, reg, kLongLoVReg);
23542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t hi = GetVReg(m, reg + 1, kLongHiVReg);
23550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          uint64_t longVal = (hi << 32) | lo;
23560399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
23570399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set8BE(buf_+1, longVal);
23580399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23590399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23600399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      default:
23610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        LOG(FATAL) << "Unknown tag " << tag_;
23620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
2364ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
23650399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // Prepend tag, which may have been updated.
23660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      JDWP::Set1(buf_, tag_);
23670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return false;
23680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
23699837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    const ScopedObjectAccessUnchecked& soa_;
23700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::FrameId frame_id_;
23710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const int slot_;
23720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    JDWP::JdwpTag tag_;
23730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    uint8_t* const buf_;
23740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const size_t width_;
2375cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    JDWP::JdwpError error_;
23760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
237700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
237800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2379a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2380221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2381221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2382221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2383cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return error;
2384221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2385cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  // TODO check thread is suspended by the debugger ?
23860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  UniquePtr<Context> context(Context::Create());
23879837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  GetLocalVisitor visitor(soa, thread, context.get(), frame_id, slot, tag, buf, width);
23880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2389cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  return visitor.error_;
23900399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
23910399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
2392cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien HertzJDWP::JdwpError Dbg::SetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot,
2393cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                   JDWP::JdwpTag tag, uint64_t value, size_t width) {
23940399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SetLocalVisitor : public StackVisitor {
23957a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    SetLocalVisitor(Thread* thread, Context* context,
23960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                    JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint64_t value,
2397ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers                    size_t width)
2398b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
23997a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context),
2400cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz          frame_id_(frame_id), slot_(slot), tag_(tag), value_(value), width_(width),
2401cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz          error_(JDWP::ERR_NONE) {}
2402ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
240300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
240400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
240500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
24060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetFrameId() != frame_id_) {
24070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        return true;  // Not our frame, carry on.
2408ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes      }
24090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // TODO: check that the tag is compatible with the actual type of the slot!
2410cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      // TODO: check slot is valid for this method or return INVALID_SLOT error.
2411ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* m = GetMethod();
2412cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      if (m->IsNative()) {
2413cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        // We can't read local value from native method.
2414cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        error_ = JDWP::ERR_OPAQUE_FRAME;
2415cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        return false;
2416cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      }
24170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      uint16_t reg = DemangleSlot(slot_, m);
24180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
24190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      switch (tag_) {
24200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_BOOLEAN:
24210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_BYTE:
24220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
24232bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
24240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_SHORT:
24260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_CHAR:
24270399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 2U);
24282bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
24290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_INT:
24312bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 4U);
24322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
24332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          break;
24340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_FLOAT:
24350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 4U);
24362bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kFloatVReg);
24370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24380399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_ARRAY:
24390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_OBJECT:
24400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_STRING:
24410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
24420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
24432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value_));
244464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes          if (o == ObjectRegistry::kInvalidObject) {
24450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            UNIMPLEMENTED(FATAL) << "return an error code when given an invalid object to store";
24460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
24472bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), kReferenceVReg);
24480399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
24490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
24500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_DOUBLE:
24512bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 8U);
24522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kDoubleLoVReg);
24532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kDoubleHiVReg);
24542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          break;
24550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_LONG:
24560399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 8U);
24572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kLongLoVReg);
24582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kLongHiVReg);
24590399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24600399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        default:
24610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          LOG(FATAL) << "Unknown tag " << tag_;
24620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
24640399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return false;
2465cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    }
24660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
24670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::FrameId frame_id_;
24680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const int slot_;
24690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::JdwpTag tag_;
24700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const uint64_t value_;
24710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const size_t width_;
2472cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    JDWP::JdwpError error_;
24730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
247400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
247500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2476a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2477221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2478221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2479221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2480cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return error;
2481221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2482cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  // TODO check thread is suspended by the debugger ?
248308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  UniquePtr<Context> context(Context::Create());
24847a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  SetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, value, width);
24850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2486cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  return visitor.error_;
2487872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2488872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
24893f52eafe5577b8489f90dc8ed5981b3455206147Sebastien HertzJDWP::ObjectId Dbg::GetThisObjectIdForEvent(mirror::Object* this_object) {
24903f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // If 'this_object' isn't already in the registry, we know that we're not looking for it, so
24913f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // there's no point adding it to the registry and burning through ids.
24923f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // When registering an event request with an instance filter, we've been given an existing object
24933f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // id so it must already be present in the registry when the event fires.
24943f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = 0;
24953f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (this_object != nullptr && gRegistry->Contains(this_object)) {
24963f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    this_id = gRegistry->Add(this_object);
24973f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
24983f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  return this_id;
24993f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
25003f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2501ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersvoid Dbg::PostLocationEvent(mirror::ArtMethod* m, int dex_pc, mirror::Object* this_object,
2502579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                            int event_flags, const JValue* return_value) {
25033f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
25043f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
25053f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
25063f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
25073f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK_EQ(m->IsStatic(), this_object == nullptr);
250891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  JDWP::JdwpLocation location;
2509cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  SetLocation(location, m, dex_pc);
251091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
25113f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // We need 'this' for InstanceOnly filters only.
25123f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = GetThisObjectIdForEvent(this_object);
2513579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  gJdwpState->PostLocationEvent(&location, this_id, event_flags, return_value);
2514872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2515872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
25163f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostFieldAccessEvent(mirror::ArtMethod* m, int dex_pc,
25173f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                               mirror::Object* this_object, mirror::ArtField* f) {
25183f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
25193f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
25203f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
25213f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
25223f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(f != nullptr);
25233f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::JdwpLocation location;
25243f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  SetLocation(location, m, dex_pc);
25253f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25263f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::RefTypeId type_id = gRegistry->AddRefType(f->GetDeclaringClass());
25273f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::FieldId field_id = ToFieldId(f);
25283f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = gRegistry->Add(this_object);
25293f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25303f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  gJdwpState->PostFieldEvent(&location, type_id, field_id, this_id, nullptr, false);
25313f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
25323f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25333f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostFieldModificationEvent(mirror::ArtMethod* m, int dex_pc,
25343f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                     mirror::Object* this_object, mirror::ArtField* f,
25353f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                     const JValue* field_value) {
25363f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
25373f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
25383f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
25393f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
25403f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(f != nullptr);
25413f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(field_value != nullptr);
25423f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::JdwpLocation location;
25433f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  SetLocation(location, m, dex_pc);
25443f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25453f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::RefTypeId type_id = gRegistry->AddRefType(f->GetDeclaringClass());
25463f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::FieldId field_id = ToFieldId(f);
25473f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = gRegistry->Add(this_object);
25483f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25493f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  gJdwpState->PostFieldEvent(&location, type_id, field_id, this_id, field_value, true);
25503f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
25513f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25523f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostException(const ThrowLocation& throw_location,
2553ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                        mirror::ArtMethod* catch_method,
255464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes                        uint32_t catch_dex_pc, mirror::Throwable* exception_object) {
2555c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
25560ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers    return;
25570ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers  }
25584740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
255962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  JDWP::JdwpLocation jdwp_throw_location;
256062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SetLocation(jdwp_throw_location, throw_location.GetMethod(), throw_location.GetDexPc());
2561d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  JDWP::JdwpLocation catch_location;
2562caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  SetLocation(catch_location, catch_method, catch_dex_pc);
2563d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
25643f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // We need 'this' for InstanceOnly filters only.
25653f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = GetThisObjectIdForEvent(throw_location.GetThis());
256664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::ObjectId exception_id = gRegistry->Add(exception_object);
256764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId exception_class_id = gRegistry->AddRefType(exception_object->GetClass());
2568d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
256962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  gJdwpState->PostException(&jdwp_throw_location, exception_id, exception_class_id, &catch_location,
257062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                            this_id);
2571872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2572872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
25732dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::PostClassPrepare(mirror::Class* c) {
2574c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
25754740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes    return;
25764740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  }
25774740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
25783d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes  // OLD-TODO - we currently always send both "verified" and "prepared" since
25794740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  // debuggers seem to like that.  There might be some advantage to honesty,
25804740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  // since the class may not yet be verified.
25814740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
25824d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag tag = GetTypeTag(c);
2583fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan Rogers  gJdwpState->PostClassPrepare(tag, gRegistry->Add(c),
2584dfb325e0ddd746cd8f7c2e3723b3a573eb7cc111Ian Rogers                               ClassHelper(c).GetDescriptor(), state);
2585872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2586872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
258762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object,
2588ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                         mirror::ArtMethod* m, uint32_t dex_pc) {
258962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) {
25902aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes    return;
259191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
259291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
25932aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes  int event_flags = 0;
25942aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes
25958696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  if (IsBreakpoint(m, dex_pc)) {
25968696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    event_flags |= kBreakpoint;
259791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
259891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
259961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // If the debugger is single-stepping one of our threads, check to
260061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // see if we're that thread and we've reached a step point.
260161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  const SingleStepControl* single_step_control = thread->GetSingleStepControl();
260261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
260361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (single_step_control->is_active) {
260461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    CHECK(!m->IsNative());
260561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    if (single_step_control->step_depth == JDWP::SD_INTO) {
260661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step into method calls.  We break when the line number
260761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // or method pointer changes.  If we're in SS_MIN mode, we
260861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // always stop.
260961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (single_step_control->method != m) {
261061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
261161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new method";
261261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (single_step_control->step_size == JDWP::SS_MIN) {
261361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
261461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new instruction";
2615bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      } else if (single_step_control->ContainsDexPc(dex_pc)) {
261661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
261761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new line";
261861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
261961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else if (single_step_control->step_depth == JDWP::SD_OVER) {
262061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step over method calls.  We break when the line number is
262161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // different and the frame depth is <= the original frame
262261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth.  (We can't just compare on the method, because we
262361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // might get unrolled past it by an exception, and it's tricky
262461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // to identify recursion.)
262561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
262661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
262761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
262861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
262961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Popped up one or more frames, always trigger.
263061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
263161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
263261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (stack_depth == single_step_control->stack_depth) {
263361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Same depth, see if we moved.
263461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control->step_size == JDWP::SS_MIN) {
26358696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          event_flags |= kSingleStep;
26368696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          VLOG(jdwp) << "SS new instruction";
2637bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz        } else if (single_step_control->ContainsDexPc(dex_pc)) {
26382435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          event_flags |= kSingleStep;
26392435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          VLOG(jdwp) << "SS new line";
264091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes        }
264161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
264261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else {
264361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      CHECK_EQ(single_step_control->step_depth, JDWP::SD_OUT);
264461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Return from the current method.  We break when the frame
264561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth pops up.
264661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
264761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // This differs from the "method exit" break in that it stops
264861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // with the PC at the next instruction in the returned-to
264961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // function, rather than the end of the returning function.
265061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
265161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
265261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
265361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
265461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
26558696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
265691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    }
265791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
265891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
265991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // If there's something interesting going on, see if it matches one
266091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // of the debugger filters.
266191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (event_flags != 0) {
2662579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, nullptr);
266391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
266491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes}
266591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
26664d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz// Process request while all mutator threads are suspended.
26674d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) {
2668138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
26694d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  switch (request.kind) {
26704d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kNothing:
26714d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      LOG(WARNING) << "Ignoring empty deoptimization request.";
26724d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
26734d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullDeoptimization:
26747ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize the world ...";
26754d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->DeoptimizeEverything();
26767ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize the world DONE";
26774d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
26784d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullUndeoptimization:
26797ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize the world ...";
26804d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->UndeoptimizeEverything();
26817ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize the world DONE";
26824d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
26834d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveDeoptimization:
26847ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.method) << " ...";
26854d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->Deoptimize(request.method);
26867ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.method) << " DONE";
26874d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
26884d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveUndeoptimization:
26897ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.method) << " ...";
26904d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->Undeoptimize(request.method);
26917ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.method) << " DONE";
26924d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
26934d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    default:
26944d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      LOG(FATAL) << "Unsupported deoptimization request kind " << request.kind;
26954d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
26964d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
26974d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz}
26984d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz
26997ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::DelayFullUndeoptimization() {
27007ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  MutexLock mu(Thread::Current(), *deoptimization_lock_);
27017ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  ++delayed_full_undeoptimization_count_;
27027ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  DCHECK_LE(delayed_full_undeoptimization_count_, full_deoptimization_event_count_);
27037ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
27047ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
27057ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::ProcessDelayedFullUndeoptimizations() {
27067ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  // TODO: avoid taking the lock twice (once here and once in ManageDeoptimization).
27077ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  {
27087ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    MutexLock mu(Thread::Current(), *deoptimization_lock_);
27097ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    while (delayed_full_undeoptimization_count_ > 0) {
27107ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      DeoptimizationRequest req;
27117ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      req.kind = DeoptimizationRequest::kFullUndeoptimization;
27127ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      req.method = nullptr;
27137ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      RequestDeoptimizationLocked(req);
27147ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      --delayed_full_undeoptimization_count_;
27157ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    }
27167ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  }
27177ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  ManageDeoptimization();
27187ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
27197ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
27204d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::RequestDeoptimization(const DeoptimizationRequest& req) {
27214d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  if (req.kind == DeoptimizationRequest::kNothing) {
27224d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    // Nothing to do.
27234d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    return;
27244d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
27254d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  MutexLock mu(Thread::Current(), *deoptimization_lock_);
27267ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  RequestDeoptimizationLocked(req);
27277ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
27287ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
27297ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) {
27304d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  switch (req.kind) {
27314d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullDeoptimization: {
27324d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK(req.method == nullptr);
27334d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      if (full_deoptimization_event_count_ == 0) {
27347ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz        VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
27357ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                   << " for full deoptimization";
27364d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz        deoptimization_requests_.push_back(req);
2737138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      }
27384d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      ++full_deoptimization_event_count_;
27394d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27404d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
27414d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullUndeoptimization: {
27424d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK(req.method == nullptr);
27434d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK_GT(full_deoptimization_event_count_, 0U);
27444d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      --full_deoptimization_event_count_;
27454d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      if (full_deoptimization_event_count_ == 0) {
27467ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz        VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
27477ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                   << " for full undeoptimization";
27484d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz        deoptimization_requests_.push_back(req);
2749138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      }
27504d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27514d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
27524d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveDeoptimization: {
27534d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK(req.method != nullptr);
27547ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
27557ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                 << " for deoptimization of " << PrettyMethod(req.method);
27564d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.push_back(req);
27574d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27584d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
27594d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveUndeoptimization: {
27604d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK(req.method != nullptr);
27617ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
27627ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                 << " for undeoptimization of " << PrettyMethod(req.method);
27634d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.push_back(req);
27644d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27654d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
27664d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    default: {
27674d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      LOG(FATAL) << "Unknown deoptimization request kind " << req.kind;
27684d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
2769138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2770138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2771138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz}
2772138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2773138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertzvoid Dbg::ManageDeoptimization() {
2774138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  Thread* const self = Thread::Current();
2775138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  {
2776138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    // Avoid suspend/resume if there is no pending request.
27774d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(self, *deoptimization_lock_);
27784d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    if (deoptimization_requests_.empty()) {
2779138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      return;
2780138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2781138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2782138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  CHECK_EQ(self->GetState(), kRunnable);
2783138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  self->TransitionFromRunnableToSuspended(kWaitingForDeoptimization);
2784138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  // We need to suspend mutator threads first.
2785138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  Runtime* const runtime = Runtime::Current();
2786138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetThreadList()->SuspendAll();
2787138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  const ThreadState old_state = self->SetStateUnsafe(kRunnable);
27884d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  {
27894d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(self, *deoptimization_lock_);
27907ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    size_t req_index = 0;
27914d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    for (const DeoptimizationRequest& request : deoptimization_requests_) {
27927ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Process deoptimization request #" << req_index++;
27934d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      ProcessDeoptimizationRequest(request);
27944d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
27954d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    deoptimization_requests_.clear();
27964d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
2797138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
2798138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetThreadList()->ResumeAll();
2799138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  self->TransitionFromSuspendedToRunnable();
2800138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz}
2801138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2802a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic bool IsMethodPossiblyInlined(Thread* self, mirror::ArtMethod* m)
2803a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2804a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  MethodHelper mh(m);
2805a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const DexFile::CodeItem* code_item = mh.GetCodeItem();
2806a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (code_item == nullptr) {
2807a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // TODO We should not be asked to watch location in a native or abstract method so the code item
2808a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // should never be null. We could just check we never encounter this case.
2809a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    return false;
2810a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  }
2811a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
2812a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
2813a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  verifier::MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader,
2814a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz                                    &mh.GetClassDef(), code_item, m->GetDexMethodIndex(), m,
2815a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz                                    m->GetAccessFlags(), false, true);
2816a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  // Note: we don't need to verify the method.
2817a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  return InlineMethodAnalyser::AnalyseMethodCode(&verifier, nullptr);
2818a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
2819138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2820a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic const Breakpoint* FindFirstBreakpointForMethod(mirror::ArtMethod* m)
2821a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_) {
2822a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  for (const Breakpoint& breakpoint : gBreakpoints) {
2823a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (breakpoint.method == m) {
2824a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      return &breakpoint;
2825138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2826a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  }
2827a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  return nullptr;
2828a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
2829138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2830a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Sanity checks all existing breakpoints on the same method.
2831a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic void SanityCheckExistingBreakpoints(mirror::ArtMethod* m, bool need_full_deoptimization)
2832a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_)  {
2833a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (kIsDebugBuild) {
2834a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    for (const Breakpoint& breakpoint : gBreakpoints) {
2835a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK_EQ(need_full_deoptimization, breakpoint.need_full_deoptimization);
2836a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
2837a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
2838a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // We should have deoptimized everything but not "selectively" deoptimized this method.
2839a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK(Runtime::Current()->GetInstrumentation()->AreAllMethodsDeoptimized());
2840a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK(!Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
2841a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
2842a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // We should have "selectively" deoptimized this method.
2843a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // Note: while we have not deoptimized everything for this method, we may have done it for
2844a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // another event.
2845a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK(Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
2846a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
2847138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2848a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
2849138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2850a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Installs a breakpoint at the specified location. Also indicates through the deoptimization
2851a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// request if we need to deoptimize.
2852a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzvoid Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
2853a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  Thread* const self = Thread::Current();
2854a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  mirror::ArtMethod* m = FromMethodId(location->method_id);
2855a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  DCHECK(m != nullptr) << "No method for method id " << location->method_id;
2856a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
2857a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  MutexLock mu(self, *Locks::breakpoint_lock_);
2858a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m);
2859a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  bool need_full_deoptimization;
2860a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (existing_breakpoint == nullptr) {
2861a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is no breakpoint on this method yet: we need to deoptimize. If this method may be
2862a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // inlined, we deoptimize everything; otherwise we deoptimize only this method.
2863a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    need_full_deoptimization = IsMethodPossiblyInlined(self, m);
2864a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
2865a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->kind = DeoptimizationRequest::kFullDeoptimization;
2866a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->method = nullptr;
2867a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
2868a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->kind = DeoptimizationRequest::kSelectiveDeoptimization;
2869a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->method = m;
2870a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
2871a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  } else {
2872a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is at least one breakpoint for this method: we don't need to deoptimize.
2873a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    req->kind = DeoptimizationRequest::kNothing;
2874a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    req->method = nullptr;
2875a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
2876a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    need_full_deoptimization = existing_breakpoint->need_full_deoptimization;
2877a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SanityCheckExistingBreakpoints(m, need_full_deoptimization);
2878138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2879a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
2880a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  gBreakpoints.push_back(Breakpoint(m, location->dex_pc, need_full_deoptimization));
2881a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": "
2882a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz             << gBreakpoints[gBreakpoints.size() - 1];
2883872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2884872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2885a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization
2886a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// request if we need to undeoptimize.
28874d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
2888ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(location->method_id);
2889a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  DCHECK(m != nullptr) << "No method for method id " << location->method_id;
2890138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2891a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
2892a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  bool need_full_deoptimization = false;
2893a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
2894a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == location->dex_pc) {
2895a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
2896a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      need_full_deoptimization = gBreakpoints[i].need_full_deoptimization;
2897a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      DCHECK_NE(need_full_deoptimization, Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
2898a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      gBreakpoints.erase(gBreakpoints.begin() + i);
2899a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      break;
2900138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2901138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2902a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m);
2903a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (existing_breakpoint == nullptr) {
2904a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is no more breakpoint on this method: we need to undeoptimize.
2905a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
2906a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // This method required full deoptimization: we need to undeoptimize everything.
2907a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->kind = DeoptimizationRequest::kFullUndeoptimization;
2908a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->method = nullptr;
2909a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
2910a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // This method required selective deoptimization: we need to undeoptimize only that method.
2911a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->kind = DeoptimizationRequest::kSelectiveUndeoptimization;
2912a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->method = m;
2913a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
2914a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  } else {
2915a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is at least one breakpoint for this method: we don't need to undeoptimize.
2916a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    req->kind = DeoptimizationRequest::kNothing;
2917a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    req->method = nullptr;
2918a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SanityCheckExistingBreakpoints(m, need_full_deoptimization);
29198696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
2920872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2921872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2922449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't
2923449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// cause suspension if the thread is the current thread.
2924449db33fafa29578df60e8a323f78d5eb6247e76Jeff Haoclass ScopedThreadSuspension {
2925449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao public:
292633e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers  ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id)
292752d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz      LOCKS_EXCLUDED(Locks::thread_list_lock_)
292833e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
2929449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      thread_(NULL),
2930449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_(JDWP::ERR_NONE),
2931449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      self_suspend_(false),
293233e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      other_suspend_(false) {
2933449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    ScopedObjectAccessUnchecked soa(self);
2934449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    {
2935449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2936449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_ = DecodeThread(soa, thread_id, thread_);
2937449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
2938449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (error_ == JDWP::ERR_NONE) {
2939449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      if (thread_ == soa.Self()) {
2940449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        self_suspend_ = true;
2941449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      } else {
2942449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
2943449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        jobject thread_peer = gRegistry->GetJObject(thread_id);
2944449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        bool timed_out;
2945d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        Thread* suspended_thread = ThreadList::SuspendThreadByPeer(thread_peer, true, true,
2946d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                                                   &timed_out);
2947449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension);
2948449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        if (suspended_thread == NULL) {
2949449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          // Thread terminated from under us while suspending.
2950449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          error_ = JDWP::ERR_INVALID_THREAD;
2951449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        } else {
2952449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          CHECK_EQ(suspended_thread, thread_);
2953449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          other_suspend_ = true;
2954449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        }
2955449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      }
2956449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
2957449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2958449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2959449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* GetThread() const {
2960449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return thread_;
2961449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2962449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2963449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError GetError() const {
2964449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return error_;
2965449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2966449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2967449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ~ScopedThreadSuspension() {
2968449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (other_suspend_) {
2969449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      Runtime::Current()->GetThreadList()->Resume(thread_, true);
2970449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
2971449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2972449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2973449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao private:
2974449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* thread_;
2975449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError error_;
2976449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool self_suspend_;
2977449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool other_suspend_;
2978449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao};
2979449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2980221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size,
298100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::JdwpStepDepth step_depth) {
2982449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* self = Thread::Current();
2983449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ScopedThreadSuspension sts(self, thread_id);
2984449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  if (sts.GetError() != JDWP::ERR_NONE) {
2985449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return sts.GetError();
29862435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
29878696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
29882435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
29892435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Work out what Method* we're in, the current line number, and how deep the stack currently
29902435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // is for step-out.
29912435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
29922435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
29930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SingleStepStackVisitor : public StackVisitor {
299461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    explicit SingleStepStackVisitor(Thread* thread, SingleStepControl* single_step_control,
299561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz                                    int32_t* line_number)
2996b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
299761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        : StackVisitor(thread, NULL), single_step_control_(single_step_control),
299861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          line_number_(line_number) {
299961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      DCHECK_EQ(single_step_control_, thread->GetSingleStepControl());
300061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->method = NULL;
300161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->stack_depth = 0;
30028696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
3003ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
300400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
300500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
300600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
300761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      mirror::ArtMethod* m = GetMethod();
30080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!m->IsRuntimeMethod()) {
300961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        ++single_step_control_->stack_depth;
301061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control_->method == NULL) {
3011ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache();
301261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->method = m;
301361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          *line_number_ = -1;
30142435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          if (dex_cache != NULL) {
30154445a7e3398a6143939168097a3aa275b734504dIan Rogers            const DexFile& dex_file = *dex_cache->GetDexFile();
301661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz            *line_number_ = dex_file.GetLineNumFromPC(m, GetDexPc());
30172435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          }
30188696433d1b3d8ba15288483b777edd888de69135Elliott Hughes        }
30198696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
3020530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
30218696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
302261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
302361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
302461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    int32_t* const line_number_;
30258696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  };
3026449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
302761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* const thread = sts.GetThread();
302861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepControl* const single_step_control = thread->GetSingleStepControl();
302961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
303061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  int32_t line_number = -1;
303161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepStackVisitor visitor(thread, single_step_control, &line_number);
30320399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
30338696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
30342435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
30352435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Find the dex_pc values that correspond to the current line, for line-based single-stepping.
30362435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
30372435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
30382435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  struct DebugCallbackContext {
3039bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    explicit DebugCallbackContext(SingleStepControl* single_step_control, int32_t line_number,
3040bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz                                  const DexFile::CodeItem* code_item)
3041bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      : single_step_control_(single_step_control), line_number_(line_number), code_item_(code_item),
304261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        last_pc_valid(false), last_pc(0) {
30432435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
30442435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
304561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) {
30462435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context);
304761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (static_cast<int32_t>(line_number) == context->line_number_) {
30482435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        if (!context->last_pc_valid) {
30492435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          // Everything from this address until the next line change is ours.
30502435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc = address;
30512435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc_valid = true;
30522435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
30532435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Otherwise, if we're already in a valid range for this line,
30542435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // just keep going (shouldn't really happen)...
30557934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      } else if (context->last_pc_valid) {  // and the line number is new
30562435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Add everything from the last entry up until here to the set
30572435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) {
305861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          context->single_step_control_->dex_pcs.insert(dex_pc);
30592435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
30602435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        context->last_pc_valid = false;
30612435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
30627934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return false;  // There may be multiple entries for any given line.
30632435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
30642435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
306561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    ~DebugCallbackContext() {
30662435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      // If the line number was the last in the position table...
30672435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      if (last_pc_valid) {
3068bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz        size_t end = code_item_->insns_size_in_code_units_;
30692435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) {
307061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->dex_pcs.insert(dex_pc);
30712435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
30722435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
30732435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
30742435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
307561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
307661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    const int32_t line_number_;
3077bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    const DexFile::CodeItem* const code_item_;
30782435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    bool last_pc_valid;
30792435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    uint32_t last_pc;
30802435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  };
308161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->dex_pcs.clear();
3082ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  mirror::ArtMethod* m = single_step_control->method;
308361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (!m->IsNative()) {
30843e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes    MethodHelper mh(m);
3085bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    const DexFile::CodeItem* const code_item = mh.GetCodeItem();
3086bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    DebugCallbackContext context(single_step_control, line_number, code_item);
3087bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
30883e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes                                    DebugCallbackContext::Callback, NULL, &context);
30893e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes  }
30902435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
30912435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
30922435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Everything else...
30932435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
30942435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
309561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_size = step_size;
309661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_depth = step_depth;
309761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->is_active = true;
30988696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
30992435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  if (VLOG_IS_ON(jdwp)) {
310061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step thread: " << *thread;
310161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step size: " << single_step_control->step_size;
310261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step depth: " << single_step_control->step_depth;
310361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->method);
310461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current line: " << line_number;
310561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->stack_depth;
31062435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step dex_pc values:";
3107bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    for (uint32_t dex_pc : single_step_control->dex_pcs) {
3108bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      VLOG(jdwp) << StringPrintf(" %#x", dex_pc);
31092435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
31102435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
31112435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
31122435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
3113872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3114872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
311561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertzvoid Dbg::UnconfigureStep(JDWP::ObjectId thread_id) {
311661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
311761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
311861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* thread;
311961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
312087118ed6f3f99e7df33214c277cf200a7b9a7499Sebastien Hertz  if (error == JDWP::ERR_NONE) {
312161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* single_step_control = thread->GetSingleStepControl();
312261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    DCHECK(single_step_control != nullptr);
3123bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    single_step_control->Clear();
312461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  }
3125872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3126872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
312745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughesstatic char JdwpTagToShortyChar(JDWP::JdwpTag tag) {
312845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  switch (tag) {
312945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    default:
313045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag);
313145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
313245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Primitives.
313345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BYTE:    return 'B';
313445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CHAR:    return 'C';
313545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_FLOAT:   return 'F';
313645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_DOUBLE:  return 'D';
313745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_INT:     return 'I';
313845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_LONG:    return 'J';
313945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_SHORT:   return 'S';
314045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_VOID:    return 'V';
314145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BOOLEAN: return 'Z';
314245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
314345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Reference types.
314445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_ARRAY:
314545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_OBJECT:
314645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_STRING:
314745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD:
314845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD_GROUP:
314945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_LOADER:
315045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_OBJECT:
315145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return 'L';
315245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  }
315345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes}
315445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
315588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id,
315688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                  JDWP::RefTypeId class_id, JDWP::MethodId method_id,
315700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  uint32_t arg_count, uint64_t* arg_values,
315800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* arg_types, uint32_t options,
315900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* pResultTag, uint64_t* pResultValue,
316000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::ObjectId* pExceptionId) {
3161d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  ThreadList* thread_list = Runtime::Current()->GetThreadList();
3162d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3163d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  Thread* targetThread = NULL;
3164d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  DebugInvokeReq* req = NULL;
316500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* self = Thread::Current();
3166d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
316700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(self);
316850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
3169221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, targetThread);
3170221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
3171221229cb523f849f165fdafbf9785010963715daElliott Hughes      LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id;
3172221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
3173d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3174d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    req = targetThread->GetInvokeReq();
3175d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (!req->ready) {
3176d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread;
3177d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      return JDWP::ERR_INVALID_THREAD;
3178d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3179d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3180d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3181d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We currently have a bug where we don't successfully resume the
3182d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * target thread if the suspend count is too deep.  We're expected to
3183d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * require one "resume" for each "suspend", but when asked to execute
3184d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a method we have to resume fully and then re-suspend it back to the
3185d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * same level.  (The easiest way to cause this is to type "suspend"
3186d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * multiple times in jdb.)
3187d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
3188d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * It's unclear what this means when the event specifies "resume all"
3189d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * and some threads are suspended more deeply than others.  This is
3190d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a rare problem, so for now we just prevent it from hanging forever
3191d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * by rejecting the method invocation request.  Without this, we will
3192d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * be stuck waiting on a suspended thread.
3193d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
319400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    int suspend_count;
319500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
319650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
319700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      suspend_count = targetThread->GetSuspendCount();
319800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
3199d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (suspend_count > 1) {
3200d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count;
32017934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return JDWP::ERR_THREAD_SUSPENDED;  // Probably not expected here.
3202d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3203d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
32043f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    JDWP::JdwpError status;
32052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id);
320664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (receiver == ObjectRegistry::kInvalidObject) {
32073f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
32083f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
320945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
32102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* thread = gRegistry->Get<mirror::Object*>(thread_id);
321164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (thread == ObjectRegistry::kInvalidObject) {
32123f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
32133f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
321445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // TODO: check that 'thread' is actually a java.lang.Thread!
321545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
32162dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_id, status);
321745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (c == NULL) {
32183f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return status;
32193f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
322045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
3221ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = FromMethodId(method_id);
322245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic() != (receiver == NULL)) {
322345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_INVALID_METHODID;
322445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
322545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic()) {
322645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (m->GetDeclaringClass() != c) {
322745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
322845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
322945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    } else {
323045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
323145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
323245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
323345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
323445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
323545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Check the argument list matches the method.
323645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    MethodHelper mh(m);
323745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (mh.GetShortyLength() - 1 != arg_count) {
323845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_ILLEGAL_ARGUMENT;
323945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
324045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    const char* shorty = mh.GetShorty();
32410920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes    const DexFile::TypeList* types = mh.GetParameterTypeList();
324245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    for (size_t i = 0; i < arg_count; ++i) {
324345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) {
324445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_ILLEGAL_ARGUMENT;
324545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
32460920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
32470920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      if (shorty[i + 1] == 'L') {
32480920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        // Did we really get an argument of an appropriate reference type?
32490920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        mirror::Class* parameter_type = mh.GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
32500920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i]);
32510920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        if (argument == ObjectRegistry::kInvalidObject) {
32520920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_INVALID_OBJECT;
32530920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
32540630ab5239a7d7be24dedbc3f66c822332446fc3Sebastien Hertz        if (argument != NULL && !argument->InstanceOf(parameter_type)) {
32550920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_ILLEGAL_ARGUMENT;
32560920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
32570920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
32580920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        // Turn the on-the-wire ObjectId into a jobject.
32590920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]);
32600920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        v.l = gRegistry->GetJObject(arg_values[i]);
32610920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      }
326245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
326345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
3264d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->receiver = receiver;
3265d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->thread = thread;
3266d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->klass = c;
3267d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->method = m;
3268d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_count = arg_count;
3269d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_values = arg_values;
3270d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->options = options;
3271d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->invoke_needed = true;
3272d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3273d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3274d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // The fact that we've released the thread list lock is a bit risky --- if the thread goes
3275d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // away we're sitting high and dry -- but we must release this before the ResumeAllThreads
3276d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // call, and it's unwise to hold it during WaitForSuspend.
3277d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3278d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
3279d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3280d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We change our (JDWP thread) status, which should be THREAD_RUNNING,
328181ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes     * so we can suspend for a GC if the invoke request causes us to
3282d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * run out of memory.  It's also a good idea to change it before locking
3283d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the invokeReq mutex, although that should never be held for long.
3284d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
328500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
3286d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
32874dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Transferring control to event thread";
3288d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    {
3289d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      MutexLock mu(self, req->lock);
3290d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3291d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
32924dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming all threads";
329300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers        thread_list->UndoDebuggerSuspensions();
3294d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      } else {
32954dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming event thread only";
3296d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes        thread_list->Resume(targetThread, true);
3297d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
3298d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3299d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      // Wait for the request to finish executing.
3300d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      while (req->invoke_needed) {
3301d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz        req->cond.Wait(self);
3302d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
3303d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
33044dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Control has returned from event thread";
3305d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3306d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* wait for thread to re-suspend itself */
3307df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom    SuspendThread(thread_id, false /* request_suspension */);
330800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
3309d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3310d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3311d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  /*
3312d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * Suspend the threads.  We waited for the target thread to suspend
3313d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * itself, so all we need to do is suspend the others.
3314d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   *
3315d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * The suspendAllThreads() call will double-suspend the event thread,
3316d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * so we want to resume the target thread once to keep the books straight.
3317d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   */
3318d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
331900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
33204dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Suspending all threads";
332100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    thread_list->SuspendAllForDebugger();
332200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
33234dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Resuming event thread to balance the count";
3324d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    thread_list->Resume(targetThread, true);
3325d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3326d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3327d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Copy the result.
3328d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pResultTag = req->result_tag;
3329d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (IsPrimitiveTag(req->result_tag)) {
3330f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = req->result_value.GetJ();
3331d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else {
3332f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = gRegistry->Add(req->result_value.GetL());
3333d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3334d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pExceptionId = req->exception;
3335d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  return req->error;
3336872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3337872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3338872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
333900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
3340d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
334181ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // We can be called while an exception is pending. We need
3342d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // to preserve that across the method invocation.
334362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SirtRef<mirror::Object> old_throw_this_object(soa.Self(), NULL);
3344ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), NULL);
334562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SirtRef<mirror::Throwable> old_exception(soa.Self(), NULL);
334662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  uint32_t old_throw_dex_pc;
334762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  {
334862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation old_throw_location;
334962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
335062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_this_object.reset(old_throw_location.GetThis());
335162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_method.reset(old_throw_location.GetMethod());
335262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_exception.reset(old_exception_obj);
335362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_dex_pc = old_throw_location.GetDexPc();
335462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->ClearException();
335562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
3356d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3357d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Translate the method through the vtable, unless the debugger wants to suppress it.
3358c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier  SirtRef<mirror::ArtMethod> m(soa.Self(), pReq->method);
3359d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz  if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver != NULL) {
336083a47d8a950867da833dfb5c3d8ddfb3d87a8cfeSebastien Hertz    mirror::ArtMethod* actual_method = pReq->klass->FindVirtualMethodForVirtualOrInterface(m.get());
3361c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier    if (actual_method != m.get()) {
3362c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier      VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m.get()) << " to " << PrettyMethod(actual_method);
3363c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier      m.reset(actual_method);
336445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
3365d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3366c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier  VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m.get())
3367d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " receiver=" << pReq->receiver
3368d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " arg_count=" << pReq->arg_count;
3369c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier  CHECK(m.get() != nullptr);
3370d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3371d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  CHECK_EQ(sizeof(jvalue), sizeof(uint64_t));
3372d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
337383a47d8a950867da833dfb5c3d8ddfb3d87a8cfeSebastien Hertz  pReq->result_value = InvokeWithJValues(soa, pReq->receiver, soa.EncodeMethod(m.get()),
337453b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers                                         reinterpret_cast<jvalue*>(pReq->arg_values));
3375d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
337662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  mirror::Throwable* exception = soa.Self()->GetException(NULL);
337762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->ClearException();
337862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  pReq->exception = gRegistry->Add(exception);
3379c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier  pReq->result_tag = BasicTagFromDescriptor(MethodHelper(m.get()).GetShorty());
3380d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (pReq->exception != 0) {
338162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    VLOG(jdwp) << "  JDWP invocation returning with exception=" << exception
338262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        << " " << exception->Dump();
3383f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    pReq->result_value.SetJ(0);
3384d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else if (pReq->result_tag == JDWP::JT_OBJECT) {
3385d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* if no exception thrown, examine object result more closely */
33869837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    JDWP::JdwpTag new_tag = TagFromObject(soa, pReq->result_value.GetL());
3387d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (new_tag != pReq->result_tag) {
33884dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes      VLOG(jdwp) << "  JDWP promoted result from " << pReq->result_tag << " to " << new_tag;
3389d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      pReq->result_tag = new_tag;
3390d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3391d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3392d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3393d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * Register the object.  We don't actually need an ObjectId yet,
3394d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * but we do need to be sure that the GC won't move or discard the
3395d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * object when we switch out of RUNNING.  The ObjectId conversion
3396d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * will add the object to the "do not touch" list.
3397d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
3398d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We can't use the "tracked allocation" mechanism here because
3399d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the object is going to be handed off to a different thread.
3400d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
3401f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    gRegistry->Add(pReq->result_value.GetL());
3402d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3403d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3404d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (old_exception.get() != NULL) {
340562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
340662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                         old_throw_dex_pc);
340762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
3408d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3409872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3410872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3411d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes/*
34124b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * "request" contains a full JDWP packet, possibly with multiple chunks.  We
3413f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * need to process each, accumulate the replies, and ship the whole thing
3414f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * back.
3415f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
3416f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * Returns "true" if we have a reply.  The reply buffer is newly allocated,
3417f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * and includes the chunk type/length, followed by the data.
3418f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
34193d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes * OLD-TODO: we currently assume that the request and reply include a single
3420f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * chunk.  If this becomes inconvenient we will need to adapt.
3421f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes */
34224b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesbool Dbg::DdmHandlePacket(JDWP::Request& request, uint8_t** pReplyBuf, int* pReplyLen) {
3423f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  Thread* self = Thread::Current();
3424f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  JNIEnv* env = self->GetJniEnv();
3425f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
34264b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t type = request.ReadUnsigned32("type");
34274b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t length = request.ReadUnsigned32("length");
34284b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
34294b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  // Create a byte[] corresponding to 'request'.
34304b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  size_t request_length = request.size();
34314b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length));
34326a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (dataArray.get() == NULL) {
34334b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    LOG(WARNING) << "byte[] allocation failed: " << request_length;
3434f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
3435f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3436f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
34374b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  env->SetByteArrayRegion(dataArray.get(), 0, request_length, reinterpret_cast<const jbyte*>(request.data()));
34384b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  request.Skip(request_length);
3439f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3440f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Run through and find all chunks.  [Currently just find the first.]
34416a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ScopedByteArrayRO contents(env, dataArray.get());
34424b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (length != request_length) {
3443ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%zd)", length, request_length);
3444f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3445f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3446f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3447f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
3448eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
3449eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                                                                 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
34504b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                                                 type, dataArray.get(), 0, length));
3451f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (env->ExceptionCheck()) {
3452f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
3453f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionDescribe();
3454f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
3455f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3456f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3457f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
34586a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (chunk.get() == NULL) {
3459f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3460f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3461f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3462f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  /*
3463f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * Pull the pieces out of the chunk.  We copy the results into a
3464f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * newly-allocated buffer that the caller can free.  We don't want to
3465f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * continue using the Chunk object because nothing has a reference to it.
3466f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
3467f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * We could avoid this by returning type/data/offset/length and having
3468f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * the caller be aware of the object lifetime issues, but that
346981ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes   * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
3470f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * if we have responses for multiple chunks.
3471f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
3472f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * So we're pretty much stuck with copying data around multiple times.
3473f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   */
3474eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
34754b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
3476eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
3477eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
3478f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
34794dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length);
34806a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (length == 0 || replyData.get() == NULL) {
3481f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3482f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3483f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
34844b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  const int kChunkHdrLen = 8;
3485f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  uint8_t* reply = new uint8_t[length + kChunkHdrLen];
3486f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (reply == NULL) {
3487f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
3488f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3489f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3490f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 0, type);
3491f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 4, length);
34926a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
3493f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3494f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyBuf = reply;
3495f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyLen = length + kChunkHdrLen;
3496f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
34974b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length);
3498f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  return true;
3499872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3500872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3501a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::DdmBroadcast(bool connect) {
35024dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
350347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
350447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  Thread* self = Thread::Current();
350550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  if (self->GetState() != kRunnable) {
350650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
350750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    /* try anyway? */
350847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
350947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
351047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  JNIEnv* env = self->GetJniEnv();
351147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
3512eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
3513eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
3514eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            event);
351547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (env->ExceptionCheck()) {
351647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
351747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionDescribe();
351847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionClear();
351947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
352047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
352147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3522872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmConnected() {
3523a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(true);
3524872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3525872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3526872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmDisconnected() {
3527a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(false);
352847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = false;
352947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
353047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
353147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes/*
35328218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes * Send a notification when a thread starts, stops, or changes its name.
353347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes *
353447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * Because we broadcast the full set of threads when the notifications are
353547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * first enabled, it's possible for "thread" to be actively executing.
353647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes */
35378218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
353847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (!gDdmThreadNotification) {
353947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    return;
354047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
354147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
35428218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  if (type == CHUNK_TYPE("THDE")) {
35438218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    uint8_t buf[4];
3544d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Set4BE(&buf[0], t->GetThreadId());
35458218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
35468218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  } else {
35478218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
354800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
35492dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    SirtRef<mirror::String> name(soa.Self(), t->GetThreadName(soa));
355047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    size_t char_count = (name.get() != NULL) ? name->GetLength() : 0;
3551725a957985171d712d5c048cc3d00ff14968784bjeffhao    const jchar* chars = (name.get() != NULL) ? name->GetCharArray()->GetData() : NULL;
355247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
355321f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    std::vector<uint8_t> bytes;
3554d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Append4BE(bytes, t->GetThreadId());
3555545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    JDWP::AppendUtf16BE(bytes, chars, char_count);
355621f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
355721f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    Dbg::DdmSendChunk(type, bytes);
355847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
355947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
356047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
356147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::DdmSetThreadNotification(bool enable) {
356200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Enable/disable thread notifications.
356347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = enable;
356447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (enable) {
356500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Suspend the VM then post thread start notifications for all threads. Threads attaching will
356600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // see a suspension in progress and block until that ends. They then post their own start
356700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // notification.
356800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    SuspendVM();
356900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    std::list<Thread*> threads;
357050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
357100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
357250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu(self, *Locks::thread_list_lock_);
357300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      threads = Runtime::Current()->GetThreadList()->GetList();
357400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
357500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
357650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      ScopedObjectAccess soa(self);
357702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (Thread* thread : threads) {
357802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR"));
357900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
358000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
358100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ResumeVM();
358247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
358347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
358447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3585a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
3586c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (IsDebuggerActive()) {
3587dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier    ScopedObjectAccessUnchecked soa(Thread::Current());
3588cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers    JDWP::ObjectId id = gRegistry->Add(t->GetPeer());
35898218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR"));
359047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
35918218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  Dbg::DdmSendThreadNotification(t, type);
359247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
359347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
359447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadStart(Thread* t) {
3595a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
359647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
359747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
359847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadDeath(Thread* t) {
3599a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
3600872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3601872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
36028218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) {
36033bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(buf != NULL);
36043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  iovec vec[1];
36053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
36063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_len = byte_count;
36073bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Dbg::DdmSendChunkV(type, vec, 1);
3608872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3609872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
361021f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) {
361121f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  DdmSendChunk(type, bytes.size(), &bytes[0]);
361221f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes}
361321f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes
3614f52935278fca8c7aa220543eef4544e3d1105d91Brian Carlstromvoid Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
36153bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (gJdwpState == NULL) {
36164dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type;
36173bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
3618cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    gJdwpState->DdmSendChunkV(type, iov, iov_count);
36193bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
3620872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3621872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3622767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesint Dbg::DdmHandleHpifChunk(HpifWhen when) {
3623767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when == HPIF_WHEN_NOW) {
36247162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(when);
3625767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return true;
3626767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3627767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3628767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
3629767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
3630767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3631767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3632767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3633767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  gDdmHpifWhen = when;
3634767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
3635767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3636767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3637767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesbool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
3638767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
3639767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
3640767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3641767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3642767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3643767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
3644767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
3645767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3646767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3647767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3648767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (native) {
3649767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhen = when;
3650767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhat = what;
3651767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  } else {
3652767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhen = when;
3653767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhat = what;
3654767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3655767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
3656767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3657767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
36587162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesvoid Dbg::DdmSendHeapInfo(HpifWhen reason) {
36597162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  // If there's a one-shot 'when', reset it.
36607162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  if (reason == gDdmHpifWhen) {
36617162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
36627162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes      gDdmHpifWhen = HPIF_WHEN_NEVER;
36637162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    }
36647162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  }
36657162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes
36667162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  /*
36677162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Chunk HPIF (client --> server)
36687162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
36697162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Heap Info. General information about the heap,
36707162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * suitable for a summary display.
36717162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
36727162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   [u4]: number of heaps
36737162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
36747162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   For each heap:
36757162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: heap ID
36767162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u8]: timestamp in ms since Unix epoch
36777162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u1]: capture reason (same as 'when' value from server)
36787162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: max heap size in bytes (-Xmx)
36797162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current heap size in bytes
36807162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of bytes allocated
36817162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of objects allocated
36827162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   */
36837162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  uint8_t heap_count = 1;
36841d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::Heap* heap = Runtime::Current()->GetHeap();
368521f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  std::vector<uint8_t> bytes;
3686545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append4BE(bytes, heap_count);
36877934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, 1);  // Heap id (bogus; we only have one heap).
3688545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append8BE(bytes, MilliTime());
3689545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append1BE(bytes, reason);
36907934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetMaxMemory());  // Max allowed heap size in bytes.
36917934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetTotalMemory());  // Current heap size in bytes.
3692b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetBytesAllocated());
3693b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetObjectsAllocated());
369421f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
369521f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes);
3696767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3697767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
36986a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgSolidity {
36996a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FREE = 0,
37006a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_HARD = 1,
37016a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SOFT = 2,
37026a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_WEAK = 3,
37036a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_PHANTOM = 4,
37046a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FINALIZABLE = 5,
37056a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SWEEP = 6,
37066a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
37076a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgKind {
37096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_OBJECT = 0,
37106a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_CLASS_OBJECT = 1,
37116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_1 = 2,
37126a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_2 = 3,
37136a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_4 = 4,
37146a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_8 = 5,
37156a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_UNKNOWN = 6,
37166a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_NATIVE = 7,
37176a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
37186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37196a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_PARTIAL (1<<7)
37206a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
37216a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
372230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogersclass HeapChunkContext {
372330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers public:
37246a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Maximum chunk size.  Obtain this from the formula:
37256a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
37266a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  HeapChunkContext(bool merge, bool native)
372730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      : buf_(16384 - 16),
372830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        type_(0),
372930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        merge_(merge) {
37306a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
37316a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    if (native) {
373230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = CHUNK_TYPE("NHSG");
37336a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    } else {
373430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
37356a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
37366a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
37376a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37386a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ~HeapChunkContext() {
373930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (p_ > &buf_[0]) {
37406a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      Flush();
37416a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
37426a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
37436a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37446a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void EnsureHeader(const void* chunk_ptr) {
374530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (!needHeader_) {
37466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      return;
37476a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
37486a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37496a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Start a new HPSx chunk.
37507934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 1);  // Heap id (bogus; we only have one heap).
37517934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write1BE(&p_, 8);  // Size of allocation unit, in bytes.
37526a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37537934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr));  // virtual address of segment start.
37547934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 0);  // offset of this piece (relative to the virtual address).
37556a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // [u4]: length of piece, in allocation units
37566a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // We won't know this until we're done, so save the offset and stuff in a dummy value.
375730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = p_;
375830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Write4BE(&p_, 0x55555555);
375930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = false;
37606a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
37616a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3762b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3763d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    if (pieceLenField_ == NULL) {
3764d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
3765d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      CHECK(needHeader_);
3766d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      return;
3767d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    }
37686a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Patch the "length of piece" field.
376930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(&buf_[0], pieceLenField_);
377030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(pieceLenField_, p_);
377130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Set4BE(pieceLenField_, totalAllocationUnits_);
37726a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
377330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]);
37746a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
37756a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
37766a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
377700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg)
3778b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3779b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
378030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes);
3781a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
3782a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
37836a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes private:
3784a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  enum { ALLOCATION_UNIT_SIZE = 8 };
3785a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
37866a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void Reset() {
378730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    p_ = &buf_[0];
378815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    startOfNextMemoryChunk_ = NULL;
378930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    totalAllocationUnits_ = 0;
379030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = true;
379130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = NULL;
37926a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
37936a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
379400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void HeapChunkCallback(void* start, void* /*end*/, size_t used_bytes)
3795b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3796b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
379730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
379830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // in the following code not to allocate memory, by ensuring buf_ is of the correct size
379915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (used_bytes == 0) {
380015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start == NULL) {
380115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            // Reset for start of new heap.
380215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
380315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
380415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
380515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Only process in use memory so that free region information
380615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // also includes dlmalloc book keeping.
3807a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
3808a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
38096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
381015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    /* If we're looking at the native heap, we'll just return
381115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     * (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks
381215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     */
381315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bool native = type_ == CHUNK_TYPE("NHSG");
381415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
381515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (startOfNextMemoryChunk_ != NULL) {
381615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Transmit any pending free memory. Native free memory of
381715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // over kMaxFreeLen could be because of the use of mmaps, so
381815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // don't report. If not free memory then start a new segment.
381915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        bool flush = true;
382015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start > startOfNextMemoryChunk_) {
382115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            const size_t kMaxFreeLen = 2 * kPageSize;
382215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeStart = startOfNextMemoryChunk_;
382315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeEnd = start;
38242d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom            size_t freeLen = reinterpret_cast<char*>(freeEnd) - reinterpret_cast<char*>(freeStart);
382515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            if (!native || freeLen < kMaxFreeLen) {
382615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), freeStart, freeLen);
382715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                flush = false;
382815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            }
382915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
383015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (flush) {
383115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
383215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
383315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
383415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
3835ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    mirror::Object* obj = reinterpret_cast<mirror::Object*>(start);
3836a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
3837a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Determine the type of this chunk.
3838a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
3839a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If it's the same, we should combine them.
384015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    uint8_t state = ExamineObject(obj, native);
384115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
384215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // allocation then the first sizeof(size_t) may belong to it.
384315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    const size_t dlMallocOverhead = sizeof(size_t);
384415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    AppendChunk(state, start, used_bytes + dlMallocOverhead);
38452d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom    startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + dlMallocOverhead;
384615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  }
384715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
384815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void AppendChunk(uint8_t state, void* ptr, size_t length)
3849b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
385015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // Make sure there's enough room left in the buffer.
385115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
385215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // 17 bytes for any header.
385315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t needed = (((length/ALLOCATION_UNIT_SIZE + 255) / 256) * 2) + 17;
385415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
385515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
385615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      Flush();
385715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
3858a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
385915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
386015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
386115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
386215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers          << needed << " bytes)";
386315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return;
386415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
386515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    EnsureHeader(ptr);
3866a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Write out the chunk description.
386715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    length /= ALLOCATION_UNIT_SIZE;   // Convert to allocation units.
386815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    totalAllocationUnits_ += length;
386915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    while (length > 256) {
387030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = state | HPSG_PARTIAL;
387130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = 255;     // length - 1
387215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      length -= 256;
3873a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
387430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    *p_++ = state;
387515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    *p_++ = length - 1;
38766a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
38776a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3878ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint8_t ExamineObject(mirror::Object* o, bool is_native_heap)
3879ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
3880a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (o == NULL) {
3881a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_FREE, 0);
3882a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
38836a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3884a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // It's an allocated chunk. Figure out what it is.
38856a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3886a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If we're looking at the native heap, we'll just return
3887a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks.
388800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (is_native_heap) {
3889a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
38906a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
38916a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
38925bfa60ffcc953340feb711ed05cf576ac821905eIan Rogers    if (!Runtime::Current()->GetHeap()->IsLiveObjectLocked(o)) {
389315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
389400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
389500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
38962dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = o->GetClass();
3897a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c == NULL) {
3898a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      // The object was probably just created but hasn't been initialized yet.
3899a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
3900a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
39016a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3902590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(c)) {
390315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
3904a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
3905a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
39066a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3907a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsClassClass()) {
3908a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
39096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
39106a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3911a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsArrayClass()) {
3912a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (o->IsObjectArray()) {
3913a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3914a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
3915a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      switch (c->GetComponentSize()) {
3916a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
3917a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
3918a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3919a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
3920a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
39216a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
39226a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3923a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
39246a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
39256a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
392630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  std::vector<uint8_t> buf_;
392730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* p_;
392830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* pieceLenField_;
392915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void* startOfNextMemoryChunk_;
393030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  size_t totalAllocationUnits_;
393130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint32_t type_;
393230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool merge_;
393330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool needHeader_;
393430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers
3935a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
3936a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes};
39376a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39386a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesvoid Dbg::DdmSendHeapSegments(bool native) {
39396a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhen when;
39406a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhat what;
39416a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (!native) {
39426a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmHpsgWhen;
39436a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmHpsgWhat;
39446a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  } else {
39456a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmNhsgWhen;
39466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmNhsgWhat;
39476a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
39486a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (when == HPSG_WHEN_NEVER) {
39496a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    return;
39506a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
39516a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39526a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Figure out what kind of chunks we'll be sending.
39536a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what);
39546a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39556a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // First, send a heap start chunk.
39566a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  uint8_t heap_id[4];
39577934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Set4BE(&heap_id[0], 1);  // Heap id (bogus; we only have one heap).
39586a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id);
39596a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3960cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Thread* self = Thread::Current();
3961cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
3962cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // To allow the Walk/InspectAll() below to exclusively-lock the
3963cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // mutator lock, temporarily release the shared access to the
3964cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // mutator lock here by transitioning to the suspended state.
3965cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Locks::mutator_lock_->AssertSharedHeld(self);
3966cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  self->TransitionFromRunnableToSuspended(kSuspended);
3967cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
39686a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Send a series of heap segment chunks.
3969a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native);
3970a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (native) {
39711d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context);
3972a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
39731d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
39741d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
39751d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
39761d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    for (It cur = spaces.begin(), end = spaces.end(); cur != end; ++cur) {
3977cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      if ((*cur)->IsMallocSpace()) {
3978cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi        (*cur)->AsMallocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
3979b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier      }
3980b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
3981e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    // Walk the large objects, these are not in the AllocSpace.
3982e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
3983a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
39846a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3985cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Shared-lock the mutator lock back.
3986cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  self->TransitionFromSuspendedToRunnable();
3987cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Locks::mutator_lock_->AssertSharedHeld(self);
3988cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
39896a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Finally, send a heap end chunk.
39906a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id);
3991767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3992767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3993b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughesstatic size_t GetAllocTrackerMax() {
3994b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#ifdef HAVE_ANDROID_OS
3995b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  // Check whether there's a system property overriding the number of records.
3996b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  const char* propertyName = "dalvik.vm.allocTrackerMax";
3997b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  char allocRecordMaxString[PROPERTY_VALUE_MAX];
3998b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  if (property_get(propertyName, allocRecordMaxString, "") > 0) {
3999b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    char* end;
4000b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    size_t value = strtoul(allocRecordMaxString, &end, 10);
4001b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (*end != '\0') {
40023e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
40033e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- invalid";
4004b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
4005b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
4006b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (!IsPowerOfTwo(value)) {
40073e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
40083e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- not power of two";
4009b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
4010b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
4011b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    return value;
4012b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  }
4013b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#endif
4014b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  return kDefaultNumAllocRecords;
4015b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes}
4016b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes
4017545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::SetAllocTrackingEnabled(bool enabled) {
4018545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (enabled) {
4019b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    {
4020b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
4021b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      if (recent_allocation_records_ == NULL) {
4022b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        alloc_record_max_ = GetAllocTrackerMax();
4023b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        LOG(INFO) << "Enabling alloc tracker (" << alloc_record_max_ << " entries of "
4024b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz            << kMaxAllocRecordStackDepth << " frames, taking "
4025b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz            << PrettySize(sizeof(AllocRecord) * alloc_record_max_) << ")";
4026b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        alloc_record_head_ = alloc_record_count_ = 0;
4027b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        recent_allocation_records_ = new AllocRecord[alloc_record_max_];
4028b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        CHECK(recent_allocation_records_ != NULL);
4029b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      }
4030545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4031fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers    Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints();
4032545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  } else {
4033fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers    Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
4034b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    {
4035b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
4036b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      delete[] recent_allocation_records_;
4037b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      recent_allocation_records_ = NULL;
4038b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    }
4039545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4040545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4041545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
40420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct AllocRecordStackVisitor : public StackVisitor {
40437a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor(Thread* thread, AllocRecord* record)
4044b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
40457a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, NULL), record(record), depth(0) {}
4046545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
404700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
404800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
404900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
4050545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if (depth >= kMaxAllocRecordStackDepth) {
4051530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return false;
4052545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4053ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = GetMethod();
40540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    if (!m->IsRuntimeMethod()) {
40550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].method = m;
40560399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].dex_pc = GetDexPc();
4057530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      ++depth;
4058545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4059530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes    return true;
4060545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4061545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4062545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  ~AllocRecordStackVisitor() {
4063545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // Clear out any unused stack trace elements.
4064545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (; depth < kMaxAllocRecordStackDepth; ++depth) {
4065545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      record->stack[depth].method = NULL;
40660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].dex_pc = 0;
4067545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4068545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4069545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4070545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  AllocRecord* record;
4071545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t depth;
4072545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
4073545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
40742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::RecordAllocation(mirror::Class* type, size_t byte_count) {
4075545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  Thread* self = Thread::Current();
4076545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  CHECK(self != NULL);
4077545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4078719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  MutexLock mu(self, *alloc_tracker_lock_);
4079545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
4080545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
4081545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4082545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4083545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Advance and clip.
4084719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (++alloc_record_head_ == alloc_record_max_) {
4085719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    alloc_record_head_ = 0;
4086545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4087545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4088545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the basics.
4089719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  AllocRecord* record = &recent_allocation_records_[alloc_record_head_];
4090545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  record->type = type;
4091545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  record->byte_count = byte_count;
4092d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  record->thin_lock_id = self->GetThreadId();
4093545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4094545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the stack trace.
40957a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor visitor(self, record);
40960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
4097545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4098719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (alloc_record_count_ < alloc_record_max_) {
4099719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    ++alloc_record_count_;
4100545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4101545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4102545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4103a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// Returns the index of the head element.
4104a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
4105a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We point at the most-recently-written record, so if gAllocRecordCount is 1
4106a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// we want to use the current element.  Take "head+1" and subtract count
4107a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// from it.
4108a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
4109a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We need to handle underflow in our circular buffer, so we add
4110b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes// gAllocRecordMax and then mask it back down.
4111719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::HeadIndex() {
4112719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  return (Dbg::alloc_record_head_ + 1 + Dbg::alloc_record_max_ - Dbg::alloc_record_count_) &
4113719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      (Dbg::alloc_record_max_ - 1);
4114545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4115545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4116545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::DumpRecentAllocations() {
411700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
4118719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  MutexLock mu(soa.Self(), *alloc_tracker_lock_);
4119545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
4120545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    LOG(INFO) << "Not recording tracked allocations";
4121545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
4122545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4123545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4124545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // "i" is the head of the list.  We want to start at the end of the
4125545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // list and move forward to the tail.
4126a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t i = HeadIndex();
4127719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  size_t count = alloc_record_count_;
4128545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4129719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  LOG(INFO) << "Tracked allocations, (head=" << alloc_record_head_ << " count=" << count << ")";
4130545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  while (count--) {
4131545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    AllocRecord* record = &recent_allocation_records_[i];
4132545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4133a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->thin_lock_id, record->byte_count)
4134545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes              << PrettyClass(record->type);
4135545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4136545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
4137ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      mirror::ArtMethod* m = record->stack[stack_frame].method;
4138545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      if (m == NULL) {
4139545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes        break;
4140545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
4141545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      LOG(INFO) << "    " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber();
4142545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4143545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4144545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // pause periodically to help logcat catch up
4145545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if ((count % 5) == 0) {
4146545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      usleep(40000);
4147545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4148545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4149719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    i = (i + 1) & (alloc_record_max_ - 1);
4150545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4151545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4152545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
41533b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid Dbg::UpdateObjectPointers(IsMarkedCallback* callback, void* arg) {
4154719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (recent_allocation_records_ != nullptr) {
4155719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
4156719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    size_t i = HeadIndex();
4157719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    size_t count = alloc_record_count_;
4158719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    while (count--) {
4159719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      AllocRecord* record = &recent_allocation_records_[i];
4160719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      DCHECK(record != nullptr);
41613b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier      record->UpdateObjectPointers(callback, arg);
4162719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      i = (i + 1) & (alloc_record_max_ - 1);
4163412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    }
4164412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
4165412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (gRegistry != nullptr) {
41663b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    gRegistry->UpdateObjectPointers(callback, arg);
4167412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
4168412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier}
4169412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
4170412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartiervoid Dbg::AllowNewObjectRegistryObjects() {
4171412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (gRegistry != nullptr) {
4172412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    gRegistry->AllowNewObjects();
4173412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
4174412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier}
4175412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
4176412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartiervoid Dbg::DisallowNewObjectRegistryObjects() {
4177412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (gRegistry != nullptr) {
4178412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    gRegistry->DisallowNewObjects();
4179412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
4180412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier}
4181412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
4182545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesclass StringTable {
4183545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes public:
4184545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  StringTable() {
4185545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4186545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
41876d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  void Add(const char* s) {
4188545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    table_.insert(s);
4189545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4190545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4191a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t IndexOf(const char* s) const {
419202e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    auto it = table_.find(s);
4193a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    if (it == table_.end()) {
4194a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes      LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
4195a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    }
4196a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    return std::distance(table_.begin(), it);
4197545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4198545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4199a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t Size() const {
4200545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return table_.size();
4201545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4202545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4203a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  void WriteTo(std::vector<uint8_t>& bytes) const {
420402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const std::string& str : table_) {
420502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      const char* s = str.c_str();
42066d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      size_t s_len = CountModifiedUtf8Chars(s);
42076d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      UniquePtr<uint16_t> s_utf16(new uint16_t[s_len]);
42086d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      ConvertModifiedUtf8ToUtf16(s_utf16.get(), s);
42096d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len);
4210545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4211545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4212545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4213545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes private:
4214a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  std::set<std::string> table_;
4215545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(StringTable);
4216545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
4217545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4218280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertzstatic const char* GetMethodSourceFile(MethodHelper* mh)
4219280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4220280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz  DCHECK(mh != nullptr);
4221280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz  const char* source_file = mh->GetDeclaringClassSourceFile();
4222280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz  return (source_file != nullptr) ? source_file : "";
4223280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz}
4224280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz
4225545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes/*
4226545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The data we send to DDMS contains everything we have recorded.
4227545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4228545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * Message header (all values big-endian):
4229545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) message header len (to allow future expansion); includes itself
4230545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) entry header len
4231545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) stack frame len
4232545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of entries
4233545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (4b) offset to string table from start of message
4234545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of class name strings
4235545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of method name strings
4236545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of source file name strings
4237545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * For each entry:
4238545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (4b) total allocation size
4239221229cb523f849f165fdafbf9785010963715daElliott Hughes *   (2b) thread id
4240545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (2b) allocated object's class name index
4241545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (1b) stack depth
4242545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   For each stack frame:
4243545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method's class name
4244545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method name
4245545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method source file
4246545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) line number, clipped to 32767; -2 if native; -1 if no source
4247545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) class name strings
4248545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) method name strings
4249545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) source file strings
4250545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4251545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * As with other DDM traffic, strings are sent as a 4-byte length
4252545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * followed by UTF-16 data.
4253545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4254545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We send up 16-bit unsigned indexes into string tables.  In theory there
4255b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes * can be (kMaxAllocRecordStackDepth * gAllocRecordMax) unique strings in
4256545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * each table, but in practice there should be far fewer.
4257545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4258545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The chief reason for using a string table here is to keep the size of
4259545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * the DDMS message to a minimum.  This is partly to make the protocol
4260545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * efficient, but also because we have to form the whole thing up all at
4261545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * once in a memory buffer.
4262545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4263545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We use separate string tables for class names, method names, and source
4264545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * files to keep the indexes small.  There will generally be no overlap
4265545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * between the contents of these tables.
4266545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes */
4267545a064aca775ba801790fced3d713d8a87bfc61Elliott HughesjbyteArray Dbg::GetRecentAllocations() {
4268545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (false) {
4269545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    DumpRecentAllocations();
4270545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4271545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
427250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* self = Thread::Current();
427346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  std::vector<uint8_t> bytes;
427446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  {
4275719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    MutexLock mu(self, *alloc_tracker_lock_);
427646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
427746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 1: generate string tables.
427846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
427946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable class_names;
428046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable method_names;
428146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable filenames;
428246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4283719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    int count = alloc_record_count_;
428446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    int idx = HeadIndex();
428546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
428646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
428746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
428846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      class_names.Add(ClassHelper(record->type).GetDescriptor());
428946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
429046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      MethodHelper mh;
429146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) {
4292ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        mirror::ArtMethod* m = record->stack[i].method;
429346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        if (m != NULL) {
429446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          mh.ChangeMethod(m);
429546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          class_names.Add(mh.GetDeclaringClassDescriptor());
429646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          method_names.Add(mh.GetName());
4297280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz          filenames.Add(GetMethodSourceFile(&mh));
429846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        }
429946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      }
4300545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4301719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      idx = (idx + 1) & (alloc_record_max_ - 1);
430246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    }
430346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4304719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    LOG(INFO) << "allocation records: " << alloc_record_count_;
430546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
430646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
430746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 2: Generate the output and store it in the buffer.
430846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
430946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
431046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) message header len (to allow future expansion); includes itself
431146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) entry header len
431246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) stack frame len
431346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kMessageHeaderLen = 15;
431446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kEntryHeaderLen = 9;
431546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kStackFrameLen = 8;
431646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kMessageHeaderLen);
431746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kEntryHeaderLen);
431846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kStackFrameLen);
431946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
432046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of entries
432146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (4b) offset to string table from start of message
432246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of class name strings
432346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of method name strings
432446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of source file name strings
4325719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    JDWP::Append2BE(bytes, alloc_record_count_);
432646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    size_t string_table_offset = bytes.size();
43277934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Append4BE(bytes, 0);  // We'll patch this later...
432846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, class_names.Size());
432946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, method_names.Size());
433046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, filenames.Size());
433146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4332719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    count = alloc_record_count_;
433346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    idx = HeadIndex();
433446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
433546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // For each entry:
433646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (4b) total allocation size
433746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) thread id
433846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) allocated object's class name index
433946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (1b) stack depth
434046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
434146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      size_t stack_depth = record->GetDepth();
4342590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      ClassHelper kh(record->type);
434346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      size_t allocated_object_class_name_index = class_names.IndexOf(kh.GetDescriptor());
434446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append4BE(bytes, record->byte_count);
434546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, record->thin_lock_id);
434646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, allocated_object_class_name_index);
434746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append1BE(bytes, stack_depth);
434846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
434946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      MethodHelper mh;
435046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
435146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // For each stack frame:
435246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method's class name
435346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method name
435446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method source file
435546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) line number, clipped to 32767; -2 if native; -1 if no source
435646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        mh.ChangeMethod(record->stack[stack_frame].method);
435746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        size_t class_name_index = class_names.IndexOf(mh.GetDeclaringClassDescriptor());
435846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        size_t method_name_index = method_names.IndexOf(mh.GetName());
4359280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz        size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(&mh));
436046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, class_name_index);
436146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, method_name_index);
436246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, file_name_index);
436346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber());
4364545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
436546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4366719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      idx = (idx + 1) & (alloc_record_max_ - 1);
4367545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4368545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
436946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) class name strings
437046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) method name strings
437146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) source file strings
437246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Set4BE(&bytes[string_table_offset], bytes.size());
437346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    class_names.WriteTo(bytes);
437446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    method_names.WriteTo(bytes);
437546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    filenames.WriteTo(bytes);
4376545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
437750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  JNIEnv* env = self->GetJniEnv();
4378545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  jbyteArray result = env->NewByteArray(bytes.size());
4379545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (result != NULL) {
4380545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
4381545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4382545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  return result;
4383545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4384545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4385872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}  // namespace art
4386