debugger.cc revision 4d466a8e4587422c989705dce3b2a19e7f0137f5
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"
39b0fa5dc7769c1e054032f39de0a3f6d6dd06f8cfIan Rogers#include "mirror/string-inl.h"
402dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/throwable.h"
416d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h"
42a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz#include "quick/inline_method_analyser.h"
4353b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers#include "reflection.h"
44a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include "safe_map.h"
4564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "scoped_thread_state_change.h"
466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#include "ScopedLocalRef.h"
47f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes#include "ScopedPrimitiveArray.h"
481f5393447b9f45be7918042d9ee7b521376de866Ian Rogers#include "sirt_ref.h"
4947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes#include "stack_indirect_reference_table.h"
50475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes#include "thread_list.h"
5162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers#include "throw_location.h"
522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "utf.h"
53a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz#include "verifier/method_verifier-inl.h"
54eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "well_known_classes.h"
55475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
563d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#ifdef HAVE_ANDROID_OS
573d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#include "cutils/properties.h"
583d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#endif
593d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom
60872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesnamespace art {
61872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
627934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstromstatic const size_t kMaxAllocRecordStackDepth = 16;  // Max 255.
637934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstromstatic const size_t kDefaultNumAllocRecords = 64*1024;  // Must be a power of 2.
64475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
65545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstruct AllocRecordStackTraceElement {
66ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* method;
670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  uint32_t dex_pc;
68545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
69412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  AllocRecordStackTraceElement() : method(nullptr), dex_pc(0) {
70412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
71412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
72b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  int32_t LineNumber() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return MethodHelper(method).GetLineNumFromDexPC(dex_pc);
74545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
75545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
76545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
77545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstruct AllocRecord {
782dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* type;
79545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t byte_count;
80545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  uint16_t thin_lock_id;
817934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  AllocRecordStackTraceElement stack[kMaxAllocRecordStackDepth];  // Unused entries have NULL method.
82545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
83545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t GetDepth() {
84545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    size_t depth = 0;
85545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    while (depth < kMaxAllocRecordStackDepth && stack[depth].method != NULL) {
86545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      ++depth;
87545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
88545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return depth;
89545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
90412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
9183c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier  void UpdateObjectPointers(IsMarkedCallback* callback, void* arg)
92412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
93412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    if (type != nullptr) {
9483c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier      type = down_cast<mirror::Class*>(callback(type, arg));
95412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    }
96412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
97412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier      mirror::ArtMethod*& m = stack[stack_frame].method;
98412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier      if (m == nullptr) {
99412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier        break;
100412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier      }
10183c8ee000d525017ead8753fce6bc1020249b96aMathieu Chartier      m = down_cast<mirror::ArtMethod*>(callback(m, arg));
102412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    }
103412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
104545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
105545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
1068696433d1b3d8ba15288483b777edd888de69135Elliott Hughesstruct Breakpoint {
107a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  // The location of this breakpoint.
108ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* method;
109a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  uint32_t dex_pc;
110a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
111a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  // Indicates whether breakpoint needs full deoptimization or selective deoptimization.
112a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  bool need_full_deoptimization;
113a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
114a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  Breakpoint(mirror::ArtMethod* method, uint32_t dex_pc, bool need_full_deoptimization)
115a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    : method(method), dex_pc(dex_pc), need_full_deoptimization(need_full_deoptimization) {}
1163b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
1173b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  void VisitRoots(RootCallback* callback, void* arg) {
1183b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    if (method != nullptr) {
1193b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier      callback(reinterpret_cast<mirror::Object**>(&method), arg, 0, kRootDebugger);
1203b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    }
1213b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
1228696433d1b3d8ba15288483b777edd888de69135Elliott Hughes};
1238696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
12400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs)
125b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
126229feb7a09317919ee51c06d1c3e715cea25da75Elliott Hughes  os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.method).c_str(), rhs.dex_pc);
1278696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return os;
1288696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
1298696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
1303f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzclass DebugInstrumentationListener FINAL : public instrumentation::InstrumentationListener {
13162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers public:
13262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  DebugInstrumentationListener() {}
13362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual ~DebugInstrumentationListener() {}
13462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1353f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodEntered(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1363f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                     uint32_t dex_pc)
1373f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
13862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
13962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
14062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
14162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
142579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    Dbg::PostLocationEvent(method, 0, this_object, Dbg::kMethodEntry, nullptr);
14362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
14462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1453f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodExited(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1463f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc, const JValue& return_value)
1473f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
14862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
14962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
15062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
15162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
152579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    Dbg::PostLocationEvent(method, dex_pc, this_object, Dbg::kMethodExit, &return_value);
15362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
15462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1553f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void MethodUnwind(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1563f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc)
1573f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
15862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    // We're not recorded to listen to this kind of event, so complain.
15962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method)
16051db44a194bafc3810a41164a8b39614f10e79dfSebastien Hertz               << " " << dex_pc;
16162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
16262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1633f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void DexPcMoved(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1643f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                  uint32_t new_dex_pc)
1653f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc);
16762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
16862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1693f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void FieldRead(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1703f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                 uint32_t dex_pc, mirror::ArtField* field)
1713f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1723f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostFieldAccessEvent(method, dex_pc, this_object, field);
17362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
1743f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
1753f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void FieldWritten(Thread* thread, mirror::Object* this_object, mirror::ArtMethod* method,
1763f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                    uint32_t dex_pc, mirror::ArtField* field, const JValue& field_value)
1773f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1783f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostFieldModificationEvent(method, dex_pc, this_object, field, &field_value);
1793f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
1803f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
1813f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
1823f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                       mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
1833f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                       mirror::Throwable* exception_object)
1843f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz      OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1853f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    Dbg::PostException(throw_location, catch_method, catch_dex_pc, exception_object);
1863f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
1873f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
1883f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz private:
1893f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DISALLOW_COPY_AND_ASSIGN(DebugInstrumentationListener);
19062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers} gDebugInstrumentationListener;
19162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1924ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes// JDWP is allowed unless the Zygote forbids it.
1934ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesstatic bool gJdwpAllowed = true;
1944ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
195c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line?
1963bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gJdwpConfigured = false;
1973bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
198c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Broken-down JDWP options. (Only valid if IsJdwpConfigured() is true.)
199376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughesstatic JDWP::JdwpOptions gJdwpOptions;
2003bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
2013bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes// Runtime JDWP state.
2023bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic JDWP::JdwpState* gJdwpState = NULL;
2033bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerConnected;  // debugger or DDMS is connected.
2043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerActive;     // debugger is making requests.
2058696433d1b3d8ba15288483b777edd888de69135Elliott Hughesstatic bool gDisposed;           // debugger called VirtualMachine.Dispose, so we should drop the connection.
2063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
20747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesstatic bool gDdmThreadNotification = false;
20847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
209767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes// DDMS GC-related settings.
210767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
211767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
212767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmHpsgWhat;
213767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
214767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmNhsgWhat;
215767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
216719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogersstatic ObjectRegistry* gRegistry = nullptr;
217475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
218545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes// Recent allocation tracking.
219719d1a33f6569864f529e5a3fff59e7bca97aad0Ian RogersMutex* Dbg::alloc_tracker_lock_ = nullptr;
220719d1a33f6569864f529e5a3fff59e7bca97aad0Ian RogersAllocRecord* Dbg::recent_allocation_records_ = nullptr;  // TODO: CircularBuffer<AllocRecord>
221719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_max_ = 0;
222719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_head_ = 0;
223719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::alloc_record_count_ = 0;
224545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
225138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz// Deoptimization support.
2264d25df3f76f864b7629ac8c0046d46997f293d8dSebastien HertzMutex* Dbg::deoptimization_lock_ = nullptr;
2274d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzstd::vector<DeoptimizationRequest> Dbg::deoptimization_requests_;
2284d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzsize_t Dbg::full_deoptimization_event_count_ = 0;
2297ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzsize_t Dbg::delayed_full_undeoptimization_count_ = 0;
230138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
231138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz// Breakpoints.
23209bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhaostatic std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_);
2338696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2343b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid DebugInvokeReq::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
2353b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier                                RootType root_type) {
2363b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (receiver != nullptr) {
2373b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(&receiver, arg, tid, root_type);
2383b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2393b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (thread != nullptr) {
2403b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(&thread, arg, tid, root_type);
2413b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2423b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (klass != nullptr) {
2433b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&klass), arg, tid, root_type);
2443b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2453b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (method != nullptr) {
2463b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&method), arg, tid, root_type);
2473b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2483b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
2493b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
250bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzvoid DebugInvokeReq::Clear() {
251bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  invoke_needed = false;
252bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  receiver = nullptr;
253bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  thread = nullptr;
254bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  klass = nullptr;
255bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  method = nullptr;
256bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
257bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
2583b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid SingleStepControl::VisitRoots(RootCallback* callback, void* arg, uint32_t tid,
2593b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier                                   RootType root_type) {
2603b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  if (method != nullptr) {
2613b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    callback(reinterpret_cast<mirror::Object**>(&method), arg, tid, root_type);
2623b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
2633b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
2643b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
265bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzbool SingleStepControl::ContainsDexPc(uint32_t dex_pc) const {
266bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  return dex_pcs.find(dex_pc) == dex_pcs.end();
267bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
268bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
269bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertzvoid SingleStepControl::Clear() {
270bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  is_active = false;
271bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  method = nullptr;
272bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz  dex_pcs.clear();
273bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz}
274bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz
2754d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid DeoptimizationRequest::VisitRoots(RootCallback* callback, void* arg) {
2764d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  if (method != nullptr) {
2774d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    callback(reinterpret_cast<mirror::Object**>(&method), arg, 0, kRootDebugger);
2784d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
2794d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz}
2804d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz
281ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic bool IsBreakpoint(const mirror::ArtMethod* m, uint32_t dex_pc)
28209bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    LOCKS_EXCLUDED(Locks::breakpoint_lock_)
283b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
28409bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
285138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
286a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes    if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == dex_pc) {
2878696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i];
2888696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      return true;
2898696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
2908696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
2918696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return false;
2928696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
2938696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
29452d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertzstatic bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread)
29552d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_) {
2969e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
2979e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // A thread may be suspended for GC; in this code, we really want to know whether
2989e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // there's a debugger suspension active.
2999e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0;
3009e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes}
3019e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
3022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status)
303b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3042dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
30564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
306436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
307436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
308436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
309436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsArrayInstance()) {
310436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_ARRAY;
311436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
312436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
313436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
314436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsArray();
315436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
316436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
3172dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError& status)
318b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
32064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
321436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
322436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
323436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
324436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsClass()) {
325436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_CLASS;
326436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
327436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
328436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
329436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsClass();
330436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
331436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
332221229cb523f849f165fdafbf9785010963715daElliott Hughesstatic JDWP::JdwpError DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, Thread*& thread)
333a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
334b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
335b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_peer = gRegistry->Get<mirror::Object*>(thread_id);
33764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_peer == NULL || thread_peer == ObjectRegistry::kInvalidObject) {
338221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't even an object.
339221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_OBJECT;
340221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
341221229cb523f849f165fdafbf9785010963715daElliott Hughes
3422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
343221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) {
344221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't a thread.
345221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_THREAD;
346221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
347221229cb523f849f165fdafbf9785010963715daElliott Hughes
348221229cb523f849f165fdafbf9785010963715daElliott Hughes  thread = Thread::FromManagedThread(soa, thread_peer);
349221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (thread == NULL) {
350221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This is a java.lang.Thread without a Thread*. Must be a zombie.
351221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_THREAD_NOT_ALIVE;
352436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
353221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
354436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
355436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
35624437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) {
35724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // JDWP deliberately uses the descriptor characters' ASCII values for its enum.
35824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // Note that by "basic" we mean that we don't get more specific than JT_OBJECT.
35924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  return static_cast<JDWP::JdwpTag>(descriptor[0]);
36024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
36124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
3629837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogersstatic JDWP::JdwpTag TagFromClass(const ScopedObjectAccessUnchecked& soa, mirror::Class* c)
363b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
36486b0010c79ef95b5333cd540b7d3af34a9f1a643Elliott Hughes  CHECK(c != NULL);
36524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsArrayClass()) {
36624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_ARRAY;
36724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
36824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsStringClass()) {
36924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_STRING;
3709837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
3719837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  if (c->IsClassClass()) {
37224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_CLASS_OBJECT;
37324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
3749837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
3759837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* thread_class = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
3769837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (thread_class->IsAssignableFrom(c)) {
3779837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_THREAD;
3789837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
3799837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
3809837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
3819837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* thread_group_class =
3829837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
3839837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (thread_group_class->IsAssignableFrom(c)) {
3849837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_THREAD_GROUP;
3859837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
3869837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
3879837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  {
3889837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    mirror::Class* class_loader_class =
3899837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ClassLoader);
3909837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    if (class_loader_class->IsAssignableFrom(c)) {
3919837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      return JDWP::JT_CLASS_LOADER;
3929837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    }
3939837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  }
3949837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  return JDWP::JT_OBJECT;
39524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
39624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
39724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes/*
39824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Objects declared to hold Object might actually hold a more specific
39924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * type.  The debugger may take a special interest in these (e.g. it
40024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * wants to display the contents of Strings), so we want to return an
40124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * appropriate tag.
40224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes *
40324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Null objects are tagged JT_OBJECT.
40424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes */
4059837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogersstatic JDWP::JdwpTag TagFromObject(const ScopedObjectAccessUnchecked& soa, mirror::Object* o)
406b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4079837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(soa, o->GetClass());
40824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
40924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
41024437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic bool IsPrimitiveTag(JDWP::JdwpTag tag) {
41124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  switch (tag) {
41224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BOOLEAN:
41324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BYTE:
41424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_CHAR:
41524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_FLOAT:
41624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_DOUBLE:
41724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_INT:
41824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_LONG:
41924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_SHORT:
42024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_VOID:
42124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return true;
42224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  default:
42324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return false;
42424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
42524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
42624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
4273bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
4283bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Handle one of the JDWP name/value pairs.
4293bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
4303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * JDWP options are:
4313bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  help: if specified, show help message and bail
4323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  transport: may be dt_socket or dt_shmem
4333bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  address: for dt_socket, "host:port", or just "port" when listening
4343bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  server: if "y", wait for debugger to attach; if "n", attach to debugger
4353bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  timeout: how long to wait for debugger to connect / listen
4363bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
4373bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Useful with server=n (these aren't supported yet):
4383bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onthrow=<exception-name>: connect to debugger when exception thrown
4393bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onuncaught=y|n: connect to debugger when uncaught exception thrown
4403bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  launch=<command-line>: launch the debugger itself
4413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
4423bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * The "transport" option is required, as is "address" if server=n.
4433bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
4443bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool ParseJdwpOption(const std::string& name, const std::string& value) {
4453bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (name == "transport") {
4463bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "dt_socket") {
447376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
4483bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "dt_android_adb") {
449376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
4503bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
4513bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP transport not supported: " << value;
4523bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4533bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4543bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "server") {
4553bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
456376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = false;
4573bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
458376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = true;
4593bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
4603bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
4613bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4623bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4633bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "suspend") {
4643bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
465376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = false;
4663bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
467376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = true;
4683bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
4693bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
4703bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4713bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4723bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "address") {
4733bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* this is either <port> or <host>:<port> */
4743bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string port_string;
475376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.host.clear();
4763bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type colon = value.find(':');
4773bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (colon != std::string::npos) {
478376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.host = value.substr(0, colon);
4793bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value.substr(colon + 1);
4803bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
4813bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value;
4823bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4833bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (port_string.empty()) {
4843bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address missing port: " << value;
4853bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4863bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4873bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    char* end;
488ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    uint64_t port = strtoul(port_string.c_str(), &end, 10);
489ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    if (*end != '\0' || port > 0xffff) {
4903bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address has junk in port field: " << value;
4913bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4923bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
493376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.port = port;
4943bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
4953bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* valid but unsupported */
4963bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
4973bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
4983bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
4993bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
5003bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5013bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
5023bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
5033bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
5053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
5063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * "transport=dt_socket,address=8000,server=y,suspend=n"
5073bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
5083bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesbool Dbg::ParseJdwpOptions(const std::string& options) {
5094dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "ParseJdwpOptions: " << options;
51047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
5113bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  std::vector<std::string> pairs;
5123bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Split(options, ',', pairs);
5133bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5143bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  for (size_t i = 0; i < pairs.size(); ++i) {
5153bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type equals = pairs[i].find('=');
5163bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (equals == std::string::npos) {
5173bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
5183bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
5193bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
5203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
5213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
5223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
523376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
5243bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP transport: " << options;
5253bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
526376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
5273bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
5283bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    return false;
5293bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
5303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
5313bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gJdwpConfigured = true;
5323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
5333bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
5343bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
535d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StartJdwp() {
536c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!gJdwpAllowed || !IsJdwpConfigured()) {
537376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    // No JDWP for you!
538376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    return;
539376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  }
540376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes
541719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  CHECK(gRegistry == nullptr);
542475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gRegistry = new ObjectRegistry;
543475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
544719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  alloc_tracker_lock_ = new Mutex("AllocTracker lock");
5454d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  deoptimization_lock_ = new Mutex("deoptimization lock", kDeoptimizationLock);
546d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // Init JDWP if the debugger is enabled. This may connect out to a
547d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger, passively listen for a debugger, or block waiting for a
548d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger.
549376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
550376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState == NULL) {
551f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // We probably failed because some other process has the port already, which means that
552f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // if we don't abort the user is likely to think they're talking to us when they're actually
553f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // talking to that other process.
5543d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Debugger thread failed to initialize";
555d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
556d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes
557d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // If a debugger has already attached, send the "welcome" message.
558d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // This may cause us to suspend all threads.
559376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState->IsActive()) {
56000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
561376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    if (!gJdwpState->PostVMStart()) {
5623d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes      LOG(WARNING) << "Failed to post 'start' message to debugger";
563d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes    }
564d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
565872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
566872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
5673b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid Dbg::VisitRoots(RootCallback* callback, void* arg) {
5684d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  {
5694d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
5704d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    for (Breakpoint& bp : gBreakpoints) {
5714d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      bp.VisitRoots(callback, arg);
5724d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
5734d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
5744d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  if (deoptimization_lock_ != nullptr) {  // only true if the debugger is started.
5754d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(Thread::Current(), *deoptimization_lock_);
5764d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    for (DeoptimizationRequest& req : deoptimization_requests_) {
5774d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      req.VisitRoots(callback, arg);
5784d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
5793b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier  }
5803b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier}
5813b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier
582d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StopJdwp() {
5830376e6b62bfb43b515b791f0a7afed2d0d8030bcSebastien Hertz  // Prevent the JDWP thread from processing JDWP incoming packets after we close the connection.
5840376e6b62bfb43b515b791f0a7afed2d0d8030bcSebastien Hertz  Disposed();
585376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  delete gJdwpState;
586719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  gJdwpState = nullptr;
587475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  delete gRegistry;
588719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  gRegistry = nullptr;
589719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  delete alloc_tracker_lock_;
590719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  alloc_tracker_lock_ = nullptr;
5914d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  delete deoptimization_lock_;
5924d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  deoptimization_lock_ = nullptr;
593872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
594872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
595767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesvoid Dbg::GcDidFinish() {
596767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
59700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
5984d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Sending heap info to DDM";
5997162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(gDdmHpifWhen);
600767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
601767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
60200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
6034d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Dumping heap to DDM";
6046a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(false);
605767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
606767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
60700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
6084d466a8e4587422c989705dce3b2a19e7f0137f5Brian Carlstrom    VLOG(jdwp) << "Dumping native heap to DDM";
6096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(true);
610767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
611767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
612767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
6134ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesvoid Dbg::SetJdwpAllowed(bool allowed) {
6144ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes  gJdwpAllowed = allowed;
6154ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes}
6164ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
617872d4ec7225444d9400d30f9027247deb91012fdElliott HughesDebugInvokeReq* Dbg::GetInvokeReq() {
618475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return Thread::Current()->GetInvokeReq();
619475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
620475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
621475fc23a4a7f35d1be87ea0b06c80df317a720acElliott HughesThread* Dbg::GetDebugThread() {
622475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
623475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
624475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
625475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::ClearWaitForEventThread() {
626475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gJdwpState->ClearWaitForEventThread();
627872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
628872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
629872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Connected() {
6303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(!gDebuggerConnected);
6314dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "JDWP has attached";
6323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gDebuggerConnected = true;
6338696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = false;
6348696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
6358696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
6368696433d1b3d8ba15288483b777edd888de69135Elliott Hughesvoid Dbg::Disposed() {
6378696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = true;
6388696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
6398696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
6408696433d1b3d8ba15288483b777edd888de69135Elliott Hughesbool Dbg::IsDisposed() {
6418696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return gDisposed;
642872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
643872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
6443f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz// All the instrumentation events the debugger is registered for.
6453f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzstatic constexpr uint32_t kListenerEvents = instrumentation::Instrumentation::kMethodEntered |
6463f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                            instrumentation::Instrumentation::kMethodExited |
6473f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                            instrumentation::Instrumentation::kDexPcMoved |
6483f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                            instrumentation::Instrumentation::kFieldRead |
6493f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                            instrumentation::Instrumentation::kFieldWritten |
6503f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                            instrumentation::Instrumentation::kExceptionCaught;
6513f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
652a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::GoActive() {
653a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Enable all debugging features, including scans for breakpoints.
654a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // This is a no-op if we're already active.
655a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Only called from the JDWP handler thread.
656a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (gDebuggerActive) {
657a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return;
658a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
659a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
660c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  {
661c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected?
66209bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
663c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    CHECK_EQ(gBreakpoints.size(), 0U);
664c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  }
665a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
666138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  {
6674d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(Thread::Current(), *deoptimization_lock_);
6684d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    CHECK_EQ(deoptimization_requests_.size(), 0U);
6694d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    CHECK_EQ(full_deoptimization_event_count_, 0U);
6707ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    CHECK_EQ(delayed_full_undeoptimization_count_, 0U);
671138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
672138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
67362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
67462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
67562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
67662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
67762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_NE(old_state, kRunnable);
678138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetInstrumentation()->EnableDeoptimization();
6793f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  runtime->GetInstrumentation()->AddListener(&gDebugInstrumentationListener, kListenerEvents);
680a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  gDebuggerActive = true;
68162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
68262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
68362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
68462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  LOG(INFO) << "Debugger is active";
685872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
686872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
687872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Disconnected() {
688234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  CHECK(gDebuggerConnected);
689234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
690c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  LOG(INFO) << "Debugger is no longer active";
691234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
69262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread
69362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener
69462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // and clear the object registry.
69562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
69662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
69762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
69862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
699aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz
700aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  // Debugger may not be active at this point.
701aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  if (gDebuggerActive) {
702aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    {
703aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // Since we're going to disable deoptimization, we clear the deoptimization requests queue.
704aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // This prevents us from having any pending deoptimization request when the debugger attaches
705aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz      // to us again while no event has been requested yet.
7064d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      MutexLock mu(Thread::Current(), *deoptimization_lock_);
7074d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.clear();
7084d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      full_deoptimization_event_count_ = 0U;
7097ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      delayed_full_undeoptimization_count_ = 0U;
710aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    }
7113f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener, kListenerEvents);
712aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    runtime->GetInstrumentation()->DisableDeoptimization();
713aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz    gDebuggerActive = false;
714aaea7343831b64f4351c9abc7493ec062adfaf53Sebastien Hertz  }
715234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gRegistry->Clear();
716234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gDebuggerConnected = false;
71762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
71862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
719872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
720872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
721c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsDebuggerActive() {
7223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gDebuggerActive;
723872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
724872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
725c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsJdwpConfigured() {
7263bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gJdwpConfigured;
727872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
728872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
729872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesint64_t Dbg::LastDebuggerActivity() {
730ca9515205010099d006ac2fac244348a1e673dcbElliott Hughes  return gJdwpState->LastDebuggerActivity();
731872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
732872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
733872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::UndoDebuggerSuspensions() {
734234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
735872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
736872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
73788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetClassName(JDWP::RefTypeId class_id) {
7382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id);
7393d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (o == NULL) {
740436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return "NULL";
7417b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
74264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
74388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id));
7443d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
7453d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (!o->IsClass()) {
7467934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    return StringPrintf("non-class %p", o);  // This is only used for debugging output anyway.
7473d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
748436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return DescriptorToName(ClassHelper(o->AsClass()).GetDescriptor());
7493d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes}
7503d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
75188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) {
752436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
7532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
754436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
755436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
7562435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
75788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  class_object_id = gRegistry->Add(c);
758436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
7598696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
7608696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
76188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) {
7623d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
7632dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
7643d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c == NULL) {
7653d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
7663d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
7673d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c->IsInterface()) {
7683d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    // http://code.google.com/p/android/issues/detail?id=20856
76988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = 0;
7703d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else {
77188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = gRegistry->Add(c->GetSuperClass());
7723d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
7733d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
774872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
775872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
776436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
7772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
77864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
779436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
780436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
781436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader()));
782436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
783872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
784872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
785436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
786436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
7872dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
788436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
789436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
7907b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
791436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
792436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask;
793436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
794de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  // Set ACC_SUPER. Dex files don't contain this flag but only classes are supposed to have it set,
795de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  // not interfaces.
796436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
797de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  if ((access_flags & kAccInterface) == 0) {
798de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban    access_flags |= kAccSuper;
799de34eea060edb4eb34d6ecd840e6e1c4c6489642Yevgeny Rouban  }
800436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
801436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAdd4BE(pReply, access_flags);
802436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
803436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
804872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
805872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
806f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott HughesJDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply)
807f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
8082dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
80964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
810f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
811f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
812f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
813f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  // Ensure all threads are suspended while we read objects' lock words.
814f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Thread* self = Thread::Current();
8155426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  CHECK_EQ(self->GetState(), kRunnable);
8165426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  self->TransitionFromRunnableToSuspended(kSuspended);
8175426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  Runtime::Current()->GetThreadList()->SuspendAll();
818f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
819f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  MonitorInfo monitor_info(o);
820f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
8215426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  Runtime::Current()->GetThreadList()->ResumeAll();
8225426324ea7a4c334cde1fdc4b8aae39f8a8e362dSebastien Hertz  self->TransitionFromSuspendedToRunnable();
823f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
824d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (monitor_info.owner_ != NULL) {
825d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer()));
826f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else {
827f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    expandBufAddObjectId(reply, gRegistry->Add(NULL));
828f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
829d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.entry_count_);
830d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.waiters_.size());
831d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) {
832d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer()));
833f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
834f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  return JDWP::ERR_NONE;
835f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes}
836f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
837734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott HughesJDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id,
838734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes                                      std::vector<JDWP::ObjectId>& monitors,
83952d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                      std::vector<uint32_t>& stack_depths) {
8404993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
8414993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
8424993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  Thread* thread;
8434993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
8444993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  if (error != JDWP::ERR_NONE) {
8454993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return error;
8464993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
8474993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
8484993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
8494993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
8504993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8514993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  struct OwnedMonitorVisitor : public StackVisitor {
8527a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    OwnedMonitorVisitor(Thread* thread, Context* context)
8534993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
8547a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context), current_stack_depth(0) {}
8554993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8564993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
8574993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // annotalysis.
8584993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
8594993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      if (!GetMethod()->IsRuntimeMethod()) {
8604993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        Monitor::VisitLocks(this, AppendOwnedMonitors, this);
861734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes        ++current_stack_depth;
8624993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      }
8634993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      return true;
8644993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
8654993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8662dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) {
8677a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg);
868734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes      visitor->monitors.push_back(owned_monitor);
869734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes      visitor->stack_depths.push_back(visitor->current_stack_depth);
8704993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
8714993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
872734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    size_t current_stack_depth;
8732dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    std::vector<mirror::Object*> monitors;
874734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    std::vector<uint32_t> stack_depths;
8754993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  };
8767a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  UniquePtr<Context> context(Context::Create());
8777a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  OwnedMonitorVisitor visitor(thread, context.get());
8784993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  visitor.WalkStack();
8794993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8804993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  for (size_t i = 0; i < visitor.monitors.size(); ++i) {
8814993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    monitors.push_back(gRegistry->Add(visitor.monitors[i]));
882734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    stack_depths.push_back(visitor.stack_depths[i]);
8834993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
8844993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
8854993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  return JDWP::ERR_NONE;
8864993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes}
8874993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
88852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien HertzJDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id,
88952d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                         JDWP::ObjectId& contended_monitor) {
890f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
891f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
892f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
893f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
894f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
895f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
896f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
897f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
898f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
899f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
900f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
901f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  contended_monitor = gRegistry->Add(Monitor::GetContendedMonitor(thread));
902f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
903f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
904f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
905f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
906ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott HughesJDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids,
907ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes                                       std::vector<uint64_t>& counts)
908ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
909412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
910412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
9112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
912ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  counts.clear();
913ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  for (size_t i = 0; i < class_ids.size(); ++i) {
914ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    JDWP::JdwpError status;
9152dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_ids[i], status);
916ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    if (c == NULL) {
917ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes      return status;
918ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    }
919ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    classes.push_back(c);
920ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    counts.push_back(0);
921ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  }
922412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CountInstances(classes, false, &counts[0]);
923ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  return JDWP::ERR_NONE;
924ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes}
925ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes
9263b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott HughesJDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, std::vector<JDWP::ObjectId>& instances)
9273b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
928412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
929412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  // We only want reachable instances, so do a GC.
930412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
9313b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  JDWP::JdwpError status;
9322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
933412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (c == nullptr) {
9343b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    return status;
9353b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
9362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
9373b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances);
9383b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
9393b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    instances.push_back(gRegistry->Add(raw_instances[i]));
9403b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
9413b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  return JDWP::ERR_NONE;
9423b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes}
9433b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes
9440cbaff584244ee767027aff35cd3c625aaee2994Elliott HughesJDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count,
9450cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes                                         std::vector<JDWP::ObjectId>& referring_objects)
9460cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
947412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  gc::Heap* heap = Runtime::Current()->GetHeap();
948412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->CollectGarbage(false);
9492dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
95064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
9510cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
9520cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
9532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
954412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  heap->GetReferringObjects(o, max_count, raw_instances);
9550cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
9560cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    referring_objects.push_back(gRegistry->Add(raw_instances[i]));
9570cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
9580cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  return JDWP::ERR_NONE;
9590cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes}
9600cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes
96164f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id)
96264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
963e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
964e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
965e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
966e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
96764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisableCollection(object_id);
96864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
96964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
97064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
97164f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id)
97264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
973e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
974e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // Unlike DisableCollection, JDWP specs do not state an invalid object causes an error. The RI
975e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // also ignores these cases and never return an error. However it's not obvious why this command
976e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // should behave differently from DisableCollection and IsCollected commands. So let's be more
977e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // strict and return an error if this happens.
978e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
979e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
980e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
98164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->EnableCollection(object_id);
98264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
98364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
98464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
98564f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected)
98664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
98765637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  if (object_id == 0) {
98865637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    // Null object id is invalid.
98965637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    return JDWP::ERR_INVALID_OBJECT;
99065637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  }
991e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  // JDWP specs state an INVALID_OBJECT error is returned if the object ID is not valid. However
99265637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  // the RI seems to ignore this and assume object has been collected.
99365637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
994e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
99565637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    is_collected = true;
99665637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz  } else {
99765637ebbb3c5bdf3f287a76df29a0340c10414c8Sebastien Hertz    is_collected = gRegistry->IsCollected(object_id);
998e96060aa2483529d087031f7cdcc0405f1ef9218Sebastien Hertz  }
99964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
100064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
100164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
100264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count)
100364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
100464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisposeObject(object_id, reference_count);
100564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
100664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
10074d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertzstatic JDWP::JdwpTypeTag GetTypeTag(mirror::Class* klass)
10084d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
10094d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  DCHECK(klass != nullptr);
10104d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  if (klass->IsArrayClass()) {
10114d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_ARRAY;
10124d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  } else if (klass->IsInterface()) {
10134d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_INTERFACE;
10144d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  } else {
10154d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    return JDWP::TT_CLASS;
10164d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  }
10174d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz}
10184d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz
101988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1020436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
10212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1022436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1023436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
10247b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1025436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
10264d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag type_tag = GetTypeTag(c);
10274d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  expandBufAdd1(pReply, type_tag);
102888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  expandBufAddRefTypeId(pReply, class_id);
1029436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1030872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1031872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
10327b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughesvoid Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) {
1033a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Get the complete list of reference classes (i.e. all classes except
1034a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // the primitive types).
1035a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Returns a newly-allocated buffer full of RefTypeId values.
1036a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  struct ClassListCreator {
1037ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) {
10387b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    }
10397b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
10402dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static bool Visit(mirror::Class* c, void* arg) {
1041a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return reinterpret_cast<ClassListCreator*>(arg)->Visit(c);
1042a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1043a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
104464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
104564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
104664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS {
1047a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (!c->IsPrimitive()) {
104864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes        classes.push_back(gRegistry->AddRefType(c));
1049a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
1050a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return true;
1051a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1052a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
10537b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    std::vector<JDWP::RefTypeId>& classes;
1054a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
1055a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
10567b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  ClassListCreator clc(classes);
1057a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc);
1058872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1059872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
106088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) {
1061436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
10622dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1063436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1064436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
10657b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
10667b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
1067a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (c->IsArrayClass()) {
1068a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
1069a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = JDWP::TT_ARRAY;
1070a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
1071a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsErroneous()) {
1072a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_ERROR;
1073a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    } else {
1074a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED;
1075a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1076a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
1077a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
1078a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
1079a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (pDescriptor != NULL) {
1080dfb325e0ddd746cd8f7c2e3723b3a573eb7cc111Ian Rogers    *pDescriptor = ClassHelper(c).GetDescriptor();
1081a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
1082436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1083872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1084872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1085c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughesvoid Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) {
10862dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
10876fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes);
10886fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  ids.clear();
10896fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  for (size_t i = 0; i < classes.size(); ++i) {
10906fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes    ids.push_back(gRegistry->Add(classes[i]));
10916fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  }
1092872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1093872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
109464f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply)
109564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
10962dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
109764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
10982435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
10992435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
11002435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
11014d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag type_tag = GetTypeTag(o->GetClass());
110264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass());
11032435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
11042435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAdd1(pReply, type_tag);
11052435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAddRefTypeId(pReply, type_id);
11062435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
11072435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1108872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1109872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1110fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan RogersJDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) {
11111fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  JDWP::JdwpError status;
11122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
11131fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  if (c == NULL) {
11141fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes    return status;
11157b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1116dfb325e0ddd746cd8f7c2e3723b3a573eb7cc111Ian Rogers  *signature = ClassHelper(c).GetDescriptor();
11171fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  return JDWP::ERR_NONE;
1118872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1119872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
112088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result) {
1121436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11222dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1123436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1124436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
11257b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1126b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz  if (c->IsProxyClass()) {
1127b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz    return JDWP::ERR_ABSENT_INFORMATION;
1128b7054baf28d4d652fbd98a94b089344a31898d53Sebastien Hertz  }
1129436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  result = ClassHelper(c).GetSourceFile();
1130436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1131872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1132872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
113388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) {
11349837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
11352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
113664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
1137546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
1138546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
11399837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  tag = TagFromObject(soa, o);
1140546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  return JDWP::ERR_NONE;
1141872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1142872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1143aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughessize_t Dbg::GetTagWidth(JDWP::JdwpTag tag) {
1144dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  switch (tag) {
1145dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_VOID:
1146dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 0;
1147dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BYTE:
1148dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BOOLEAN:
1149dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 1;
1150dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CHAR:
1151dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_SHORT:
1152dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 2;
1153dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_FLOAT:
1154dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_INT:
1155dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 4;
1156dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_ARRAY:
1157dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_OBJECT:
1158dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_STRING:
1159dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD:
1160dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD_GROUP:
1161dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_LOADER:
1162dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_OBJECT:
1163dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return sizeof(JDWP::ObjectId);
1164dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_DOUBLE:
1165dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_LONG:
1166dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 8;
1167dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  default:
11683d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Unknown tag " << tag;
1169dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return -1;
1170dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1171872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1172872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
117388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int& length) {
11743d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
11752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
11763d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (a == NULL) {
11773d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
117824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
11793d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  length = a->GetLength();
11803d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1181872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1182872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
118388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) {
11843d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
11852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
11869837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  if (a == nullptr) {
11873d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
11883d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
118924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
119024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) {
119124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
11923d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
119324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
11946d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor());
119524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
119624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
11973d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd1(pReply, tag);
11983d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd4BE(pReply, count);
11993d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
120024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (IsPrimitiveTag(tag)) {
120124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    size_t width = GetTagWidth(tag);
120224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    uint8_t* dst = expandBufAddSpace(pReply, count * width);
120324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    if (width == 8) {
1204ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t), 0));
120524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]);
120624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 4) {
1207ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t), 0));
120824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]);
120924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 2) {
1210ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t), 0));
121124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]);
121224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else {
1213ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t), 0));
121424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      memcpy(dst, &src[offset * width], count * width);
121524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
121624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  } else {
12179837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
12182dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>();
121924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    for (int i = 0; i < count; ++i) {
12202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* element = oa->Get(offset + i);
12219837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers      JDWP::JdwpTag specific_tag = (element != nullptr) ? TagFromObject(soa, element)
12229837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers                                                        : tag;
122324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAdd1(pReply, specific_tag);
122424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAddObjectId(pReply, gRegistry->Add(element));
122524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
122624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
122724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
12283d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1229872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1230872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1231ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogerstemplate <typename T>
1232ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersstatic void CopyArrayData(mirror::Array* a, JDWP::Request& src, int offset, int count)
1233ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    NO_THREAD_SAFETY_ANALYSIS {
1234ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  // TODO: fix when annotalysis correctly handles non-member functions.
12354b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  DCHECK(a->GetClass()->IsPrimitiveArray());
12364b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
1237ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  T* dst = reinterpret_cast<T*>(a->GetRawData(sizeof(T), offset));
12384b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  for (int i = 0; i < count; ++i) {
12394b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    *dst++ = src.ReadValue(sizeof(T));
12404b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  }
12414b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
12424b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
124388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count,
12444b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                      JDWP::Request& request)
1245b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
12463d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
12474b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  mirror::Array* dst = DecodeArray(array_id, status);
12484b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (dst == NULL) {
12493d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
12503d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
1251f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
12524b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) {
1253f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
12543d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
1255f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
12561d66e88ea74f7d85f75db7f145fe955d5f0f6872nikolay serdjuk  ClassHelper ch(dst->GetClass());
12571d66e88ea74f7d85f75db7f145fe955d5f0f6872nikolay serdjuk  const char* descriptor = ch.GetDescriptor();
1258fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor + 1);
1259f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
1260f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  if (IsPrimitiveTag(tag)) {
1261f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    size_t width = GetTagWidth(tag);
1262f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    if (width == 8) {
12634b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint64_t>(dst, request, offset, count);
1264f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 4) {
12654b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint32_t>(dst, request, offset, count);
1266f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 2) {
12674b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint16_t>(dst, request, offset, count);
1268f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else {
12694b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint8_t>(dst, request, offset, count);
1270f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1271f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  } else {
12724b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>();
1273f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    for (int i = 0; i < count; ++i) {
12744b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      JDWP::ObjectId id = request.ReadObjectId();
12752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
127664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes      if (o == ObjectRegistry::kInvalidObject) {
1277436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
1278436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes      }
1279d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      oa->Set<false>(offset + i, o);
1280f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1281f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1282f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
12833d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1284872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1285872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
12867b3cdfcca472b779cf8745fb8460935e56229f11Elliott HughesJDWP::ObjectId Dbg::CreateString(const std::string& str) {
12872dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  return gRegistry->Add(mirror::String::AllocFromModifiedUtf8(Thread::Current(), str.c_str()));
1288872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1289872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
129088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) {
1291436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
12922dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1293436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1294436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
12957b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
129650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  new_object = gRegistry->Add(c->AllocObject(Thread::Current()));
1297436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1298872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1299872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1300bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes/*
1301bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".
1302bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes */
130388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length,
130400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                       JDWP::ObjectId& new_array) {
1305436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
13062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(array_class_id, status);
1307436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1308436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
13097b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
13106fac447555dc94a935b78198479cce645c837b89Ian Rogers  new_array = gRegistry->Add(mirror::Array::Alloc<true>(Thread::Current(), c, length,
13116fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                        c->GetComponentSize(),
13126fac447555dc94a935b78198479cce645c837b89Ian Rogers                                                        Runtime::Current()->GetHeap()->GetCurrentAllocator()));
1313436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1314872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1315872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
131688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesbool Dbg::MatchType(JDWP::RefTypeId instance_class_id, JDWP::RefTypeId class_id) {
1317436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
13182dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c1 = DecodeClass(instance_class_id, status);
1319a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  CHECK(c1 != NULL);
13202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c2 = DecodeClass(class_id, status);
1321a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  CHECK(c2 != NULL);
1322123756a041baf8421ed933312605daa5ef082f6fSebastien Hertz  return c2->IsAssignableFrom(c1);
1323872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1324872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1325ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::FieldId ToFieldId(const mirror::ArtField* f)
1326b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1327590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
132803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f));
132903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
133003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1331ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::MethodId ToMethodId(const mirror::ArtMethod* m)
1332b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1333590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
133403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m));
133503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
133603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1337ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtField* FromFieldId(JDWP::FieldId fid)
1338b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1339590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
1340ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtField*>(static_cast<uintptr_t>(fid));
1341aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes}
1342aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1343ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtMethod* FromMethodId(JDWP::MethodId mid)
1344b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1345590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
1346ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtMethod*>(static_cast<uintptr_t>(mid));
134703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
134803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1349ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic void SetLocation(JDWP::JdwpLocation& location, mirror::ArtMethod* m, uint32_t dex_pc)
1350b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
135191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (m == NULL) {
135291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    memset(&location, 0, sizeof(location));
135391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  } else {
13542dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = m->GetDeclaringClass();
13554d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz    location.type_tag = GetTypeTag(c);
1356cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    location.class_id = gRegistry->AddRefType(c);
1357748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    location.method_id = ToMethodId(m);
1358cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    location.dex_pc = (m->IsNative() || m->IsProxyMethod()) ? static_cast<uint64_t>(-1) : dex_pc;
135991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
1360d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes}
1361d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
1362a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetMethodName(JDWP::MethodId method_id)
1363b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1364ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
13656d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  return MethodHelper(m).GetName();
1366872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1367872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1368a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetFieldName(JDWP::FieldId field_id)
1369a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1370ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
1371a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes  return FieldHelper(f).GetName();
1372a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes}
1373a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes
1374a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes/*
1375a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * Augment the access flags for synthetic methods and fields by setting
1376a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * the (as described by the spec) "0xf0000000 bit".  Also, strip out any
1377a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * flags not specified by the Java programming language.
1378a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes */
1379a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughesstatic uint32_t MangleAccessFlags(uint32_t accessFlags) {
1380a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  accessFlags &= kAccJavaFlagsMask;
1381a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  if ((accessFlags & kAccSynthetic) != 0) {
1382a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    accessFlags |= 0xf0000000;
1383a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1384a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return accessFlags;
1385872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1386872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1387dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes/*
1388b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come first. Debuggers
1389b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * expect slots to begin with arguments, but dex code places them at
1390b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * the end.
1391dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes */
1392b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Haostatic uint16_t MangleSlot(uint16_t slot, mirror::ArtMethod* m)
1393b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1394b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
1395cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1396cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // We should not get here for a method without code (native, proxy or abstract). Log it and
1397cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // return the slot as is since all registers are arguments.
1398cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    LOG(WARNING) << "Trying to mangle slot for method without code " << PrettyMethod(m);
1399cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return slot;
1400cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1401b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1402b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1403b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot >= locals_size) {
1404b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - locals_size;
1405b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1406b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + ins_size;
1407dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1408dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1409dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1410b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao/*
1411b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come last. Reverts
1412b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * slots to dex style argument placement.
1413b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao */
1414ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic uint16_t DemangleSlot(uint16_t slot, mirror::ArtMethod* m)
1415b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1416b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
1417cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1418cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // We should not get here for a method without code (native, proxy or abstract). Log it and
1419cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    // return the slot as is since all registers are arguments.
1420cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    LOG(WARNING) << "Trying to demangle slot for method without code " << PrettyMethod(m);
1421cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return slot;
1422cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1423b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1424b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1425b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot < ins_size) {
1426b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + locals_size;
1427b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1428b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - ins_size;
1429dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1430dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1431dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
143288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1433436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1435436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1436436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
14377b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1438a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1439a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t instance_field_count = c->NumInstanceFields();
1440a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t static_field_count = c->NumStaticFields();
1441a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1442a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, instance_field_count + static_field_count);
1443a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1444a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < instance_field_count + static_field_count; ++i) {
1445ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count);
14466d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    FieldHelper fh(f);
1447a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddFieldId(pReply, ToFieldId(f));
14486d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, fh.GetName());
14496d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, fh.GetTypeDescriptor());
1450c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1451a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1452a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1453a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1454a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags()));
1455a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1456436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1457a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1458a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
145988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic,
146000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                           JDWP::ExpandBuf* pReply) {
1461436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14622dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1463436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1464436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
14657b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1466a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1467a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t direct_method_count = c->NumDirectMethods();
1468a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t virtual_method_count = c->NumVirtualMethods();
1469a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1470a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, direct_method_count + virtual_method_count);
1471a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1472a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) {
1473ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
14746d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    MethodHelper mh(m);
1475a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddMethodId(pReply, ToMethodId(m));
14766d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, mh.GetName());
1477d91d6d6a80748f277fd938a412211e5af28913b1Ian Rogers    expandBufAddUtf8String(pReply, mh.GetSignature().ToString());
1478c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1479a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1480a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1481a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1482a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags()));
1483a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1484436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1485a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1486a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
148788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1488436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
14892dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1490436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1491436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
14927b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1493436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
1494436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  ClassHelper kh(c);
1495d24e264ff85ad8c6f142ac6d33055fdc1881fa2fIan Rogers  size_t interface_count = kh.NumDirectInterfaces();
1496a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, interface_count);
1497a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < interface_count; ++i) {
149864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    expandBufAddRefTypeId(pReply, gRegistry->AddRefType(kh.GetDirectInterface(i)));
1499a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1500436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1501872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1502872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
150388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply)
1504b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
150503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  struct DebugCallbackContext {
150603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    int numItems;
150703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    JDWP::ExpandBuf* pReply;
150803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
15092435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    static bool Callback(void* context, uint32_t address, uint32_t line_number) {
151003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
151103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      expandBufAdd8BE(pContext->pReply, address);
15122435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      expandBufAdd4BE(pContext->pReply, line_number);
151303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      pContext->numItems++;
1514f2910eef247b45ce1d489e323b36b5de6b6157aaSebastien Hertz      return false;
151503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
151603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
1517ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
15186d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  MethodHelper mh(m);
1519cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  const DexFile::CodeItem* code_item = mh.GetCodeItem();
152003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  uint64_t start, end;
1521cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item == nullptr) {
1522cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    DCHECK(m->IsNative() || m->IsProxyMethod());
152303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = -1;
152403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    end = -1;
152503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  } else {
152603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = 0;
152714f0db92225d34622fa5cb1a6dc9287334aaf6c7jeffhao    // Return the index of the last instruction
1528cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    end = code_item->insns_size_in_code_units_ - 1;
152903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  }
153003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
153103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, start);
153203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, end);
153303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
153403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  // Add numLines later
153503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  size_t numLinesOffset = expandBufGetLength(pReply);
153603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd4BE(pReply, 0);
153703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
153803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  DebugCallbackContext context;
153903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.numItems = 0;
154003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.pReply = pReply;
154103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1542cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item != nullptr) {
1543cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
1544cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                    DebugCallbackContext::Callback, NULL, &context);
1545cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
154603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
154703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems);
1548872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1549872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
155088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1551dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  struct DebugCallbackContext {
1552b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    mirror::ArtMethod* method;
1553dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    JDWP::ExpandBuf* pReply;
1554c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    size_t variable_count;
1555c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    bool with_generic;
1556dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1557b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, const char* name, const char* descriptor, const char* signature)
1558b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1559dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1560dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1561b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff 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));
1562dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1563b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao      slot = MangleSlot(slot, pContext->method);
156468fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
1565dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd8BE(pContext->pReply, startAddress);
1566dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, name);
1567dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, descriptor);
1568c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      if (pContext->with_generic) {
1569dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes        expandBufAddUtf8String(pContext->pReply, signature);
1570dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      }
1571dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, endAddress - startAddress);
1572dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, slot);
1573dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1574c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      ++pContext->variable_count;
1575dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    }
1576dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  };
1577ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
15786d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  MethodHelper mh(m);
1579dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1580c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // arg_count considers doubles and longs to take 2 units.
1581c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // variable_count considers everything to take 1 unit.
15826d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  std::string shorty(mh.GetShorty());
1583ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  expandBufAdd4BE(pReply, mirror::ArtMethod::NumArgRegisters(shorty));
1584dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1585c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // We don't know the total number of variables yet, so leave a blank and update it later.
1586c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  size_t variable_count_offset = expandBufGetLength(pReply);
1587dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  expandBufAdd4BE(pReply, 0);
1588dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1589dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  DebugCallbackContext context;
1590b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  context.method = m;
1591dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  context.pReply = pReply;
1592c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.variable_count = 0;
1593c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.with_generic = with_generic;
1594dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1595cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  const DexFile::CodeItem* code_item = mh.GetCodeItem();
1596cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  if (code_item != nullptr) {
1597cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL,
1598cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                    DebugCallbackContext::Callback, &context);
1599cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  }
1600dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1601c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count);
1602872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1603872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1604579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value,
1605579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                                  JDWP::ExpandBuf* pReply) {
1606579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  mirror::ArtMethod* m = FromMethodId(method_id);
1607579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  JDWP::JdwpTag tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty());
1608579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  OutputJValue(tag, return_value, pReply);
1609579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1610579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
16113f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::OutputFieldValue(JDWP::FieldId field_id, const JValue* field_value,
16123f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                           JDWP::ExpandBuf* pReply) {
16133f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  mirror::ArtField* f = FromFieldId(field_id);
16143f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
16153f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  OutputJValue(tag, field_value, pReply);
16163f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
16173f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
16189777ba230c83a0edcbda2cf7b208339e77bf171bElliott HughesJDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id,
16199777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes                                  std::vector<uint8_t>& bytecodes)
16209777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1621ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
16229777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  if (m == NULL) {
16239777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    return JDWP::ERR_INVALID_METHODID;
16249777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
16259777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  MethodHelper mh(m);
16269777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const DexFile::CodeItem* code_item = mh.GetCodeItem();
16279777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  size_t byte_count = code_item->insns_size_in_code_units_ * 2;
16289777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_);
16299777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* end = begin + byte_count;
16309777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  for (const uint8_t* p = begin; p != end; ++p) {
16319777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    bytecodes.push_back(*p);
16329777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
16339777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  return JDWP::ERR_NONE;
16349777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes}
16359777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes
163688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) {
163788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
1638872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1639872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
164088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) {
164188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
1642872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1643872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
164488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id,
164588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                         JDWP::FieldId field_id, JDWP::ExpandBuf* pReply,
164600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         bool is_static)
1647b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
16480cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  JDWP::JdwpError status;
16492dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(ref_type_id, status);
165088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  if (ref_type_id != 0 && c == NULL) {
16510cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    return status;
16520cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
16530cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
16542dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
165564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
16563f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
16573f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1658ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
16590cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
16602dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* receiver_class = c;
16610cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class == NULL && o != NULL) {
16620cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    receiver_class = o->GetClass();
16630cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
16640cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we give up now if receiver_class is NULL?
16650cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) {
16660cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class);
16673f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_FIELDID;
16683f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1669aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
16700cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
16710cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
16720cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
16730cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
16740cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
16750cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
16760cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
16770cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
16780cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
16790cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
16800cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
16810dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
16820dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
16830dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
16840cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
16856d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
1686579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  JValue field_value;
1687579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (tag == JDWP::JT_VOID) {
1688579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    LOG(FATAL) << "Unknown tag: " << tag;
1689579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (!IsPrimitiveTag(tag)) {
1690579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetL(f->GetObject(o));
1691579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1692579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetJ(f->Get64(o));
1693aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
1694579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetI(f->Get32(o));
1695aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
1696579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  Dbg::OutputJValue(tag, &field_value, pReply);
1697579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
16983f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  return JDWP::ERR_NONE;
1699872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1700872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
170188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id,
170200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ExpandBuf* pReply) {
170388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(0, object_id, field_id, pReply, false);
17043f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
17053f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
170688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, JDWP::ExpandBuf* pReply) {
170788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true);
17083f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
17093f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
171088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id,
171100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         uint64_t value, int width, bool is_static)
1712b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
17132dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
171464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
17153f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
17163f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1717ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
17180cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
17190cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
17200cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
17210cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
17220cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
17230cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
17240cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
17250cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
17260cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
17270cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
17280cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
17293f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
17300dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
17310dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
17320dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
1733aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
17346d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
1735aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1736aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  if (IsPrimitiveTag(tag)) {
1737aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
17381bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_EQ(width, 8);
1739d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      // Debugging can't use transactional mode (runtime only).
1740d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      f->Set64<false>(o, value);
1741aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    } else {
17421bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_LE(width, 4);
1743d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      // Debugging can't use transactional mode (runtime only).
1744d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz      f->Set32<false>(o, value);
1745aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    }
1746aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
17472dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* v = gRegistry->Get<mirror::Object*>(value);
174864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (v == ObjectRegistry::kInvalidObject) {
17493d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes      return JDWP::ERR_INVALID_OBJECT;
17503d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    }
17513f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    if (v != NULL) {
17522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* field_type = FieldHelper(f).GetType();
17533f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      if (!field_type->IsAssignableFrom(v->GetClass())) {
17543f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
17553f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      }
17563f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
1757d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    // Debugging can't use transactional mode (runtime only).
1758d2fe10a3a34af171bf1631219cd2d6ff6b7778b5Sebastien Hertz    f->SetObject<false>(o, v);
1759aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
17603d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
17613d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1762872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1763872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
176488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value,
176500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   int width) {
176688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(object_id, field_id, value, width, false);
1767872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1768872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
176988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) {
177088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(0, field_id, value, width, true);
1771872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1772872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
177388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::StringToUtf8(JDWP::ObjectId string_id) {
17742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = gRegistry->Get<mirror::String*>(string_id);
177568fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  return s->ToModifiedUtf8();
1776872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1777872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1778579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) {
1779579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (IsPrimitiveTag(tag)) {
1780579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAdd1(pReply, tag);
1781579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) {
1782579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd1(pReply, return_value->GetI());
1783579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) {
1784579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd2BE(pReply, return_value->GetI());
1785579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) {
1786579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd4BE(pReply, return_value->GetI());
1787579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1788579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd8BE(pReply, return_value->GetJ());
1789579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else {
1790579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      CHECK_EQ(tag, JDWP::JT_VOID);
1791579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    }
1792579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else {
17939837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
1794579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    mirror::Object* value = return_value->GetL();
17959837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    expandBufAdd1(pReply, TagFromObject(soa, value));
1796579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAddObjectId(pReply, gRegistry->Add(value));
1797579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  }
1798579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1799579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
1800221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string& name) {
1801a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  ScopedObjectAccessUnchecked soa(Thread::Current());
1802a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1803221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1804221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1805221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) {
1806221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1807a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1808221229cb523f849f165fdafbf9785010963715daElliott Hughes
1809221229cb523f849f165fdafbf9785010963715daElliott Hughes  // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName.
18102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
1811ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* java_lang_Thread_name_field =
18122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      soa.DecodeField(WellKnownClasses::java_lang_Thread_name);
18132dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s =
18142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object));
1815221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (s != NULL) {
1816221229cb523f849f165fdafbf9785010963715daElliott Hughes    name = s->ToModifiedUtf8();
1817221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1818221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1819872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1820872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1821221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
182200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
18232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
182464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_object == ObjectRegistry::kInvalidObject) {
18252435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
18262435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
18279837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroup");
18282435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Okay, so it's an object, but is it actually a thread?
182950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1830221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1831221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1832221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1833221229cb523f849f165fdafbf9785010963715daElliott Hughes    // Zombie threads are in the null group.
1834221229cb523f849f165fdafbf9785010963715daElliott Hughes    expandBufAddObjectId(pReply, JDWP::ObjectId(0));
183552d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    error = JDWP::ERR_NONE;
183652d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz  } else if (error == JDWP::ERR_NONE) {
183752d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
183852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    CHECK(c != nullptr);
183952d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::ArtField* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;");
184052d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    CHECK(f != NULL);
184152d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    mirror::Object* group = f->GetObject(thread_object);
184252d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    CHECK(group != NULL);
184352d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    JDWP::ObjectId thread_group_id = gRegistry->Add(group);
184452d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz    expandBufAddObjectId(pReply, thread_group_id);
18452435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
18469837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
184752d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz  return error;
1848872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1849872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
185088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id) {
185100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
18522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
18539837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(thread_group != nullptr);
18549837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroupName");
18559837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
18569837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(c != nullptr);
1857ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("name", "Ljava/lang/String;");
1858499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(f != NULL);
18592dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group));
18609837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
1861499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  return s->ToModifiedUtf8();
1862872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1863872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
186488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id) {
18659837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
18662dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
18679837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(thread_group != nullptr);
18689837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  const char* old_cause = soa.Self()->StartAssertNoThreadSuspension("Debugger: GetThreadGroupParent");
18699837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  mirror::Class* c = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_ThreadGroup);
18709837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  CHECK(c != nullptr);
1871ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;");
18724e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  CHECK(f != NULL);
18732dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* parent = f->GetObject(thread_group);
18749837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  soa.Self()->EndAssertNoThreadSuspension(old_cause);
18754e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  return gRegistry->Add(parent);
1876872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1877872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1878872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetSystemThreadGroupId() {
187900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
1880ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup);
18812dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
1882365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
1883872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1884872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1885872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetMainThreadGroupId() {
188600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1887ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup);
18882dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
1889365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
1890872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1891872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1892920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff HaoJDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) {
1893920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  switch (state) {
1894920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kBlocked:
1895920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_MONITOR;
1896920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kNative:
1897920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kRunnable:
1898920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSuspended:
1899920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_RUNNING;
1900920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSleeping:
1901920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_SLEEPING;
1902920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kStarting:
1903920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTerminated:
1904920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_ZOMBIE;
1905920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTimedWaiting:
1906920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSend:
1907920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSuspension:
1908920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerToAttach:
1909138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    case kWaitingForDeoptimization:
1910920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForGcToComplete:
1911920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForCheckPointsToRun:
1912920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForJniOnLoad:
1913920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForSignalCatcherOutput:
1914920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainDebuggerLoop:
1915920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainSignalCatcherLoop:
1916920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingPerformingGc:
1917920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaiting:
1918920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_WAIT;
1919920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      // Don't add a 'default' here so the compiler can spot incompatible enum changes.
1920920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  }
1921920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  LOG(FATAL) << "Unknown thread state: " << state;
1922920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  return JDWP::TS_ZOMBIE;
1923920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao}
1924920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao
192552d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien HertzJDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus,
192652d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz                                     JDWP::JdwpSuspendStatus* pSuspendStatus) {
192700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1928499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
19299e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED;
19309e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
193150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1932221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1933221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1934221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1935221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1936221229cb523f849f165fdafbf9785010963715daElliott Hughes      *pThreadStatus = JDWP::TS_ZOMBIE;
1937221229cb523f849f165fdafbf9785010963715daElliott Hughes      return JDWP::ERR_NONE;
1938221229cb523f849f165fdafbf9785010963715daElliott Hughes    }
1939221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1940499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  }
1941499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
19429e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  if (IsSuspendedForDebugger(soa, thread)) {
19439e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED;
19449e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  }
194500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
1946920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  *pThreadStatus = ToJdwpThreadStatus(thread->GetState());
1947221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1948872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1949872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1950221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
195100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
195250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1953221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1954221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1955221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1956221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
19572435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
195850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
195900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  expandBufAdd4BE(pReply, thread->GetDebugSuspendCount());
19602435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1961872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1962872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1963f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott HughesJDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) {
1964f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccess soa(Thread::Current());
1965f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1966f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
1967f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1968f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
1969f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
1970f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
1971dd7624d2b9e599d57762d12031b10b89defc9807Ian Rogers  thread->Interrupt(soa.Self());
1972f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
1973f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
1974f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
1975caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughesvoid Dbg::GetThreads(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& thread_ids) {
1976365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  class ThreadListVisitor {
1977365c10235438607541fa2259a5fec48061b90bd8Ian Rogers   public:
19782dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    ThreadListVisitor(const ScopedObjectAccessUnchecked& soa, mirror::Object* desired_thread_group,
197900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                      std::vector<JDWP::ObjectId>& thread_ids)
1980b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
19810dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        : soa_(soa), desired_thread_group_(desired_thread_group), thread_ids_(thread_ids) {}
1982365c10235438607541fa2259a5fec48061b90bd8Ian Rogers
1983a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    static void Visit(Thread* t, void* arg) {
1984a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t);
1985a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1986a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
198700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
198800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
198900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void Visit(Thread* t) NO_THREAD_SAFETY_ANALYSIS {
1990a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (t == Dbg::GetDebugThread()) {
1991a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and
1992a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        // query all threads, so it's easier if we just don't tell them about this thread.
1993a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
1994a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
19952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* peer = t->GetPeer();
19960dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      if (IsInDesiredThreadGroup(peer)) {
1997120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers        thread_ids_.push_back(gRegistry->Add(peer));
1998a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
1999a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
2000a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
2001365c10235438607541fa2259a5fec48061b90bd8Ian Rogers   private:
20022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    bool IsInDesiredThreadGroup(mirror::Object* peer)
20030dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20040dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      // peer might be NULL if the thread is still starting up.
20050dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      if (peer == NULL) {
20060dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // We can't tell the debugger about this thread yet.
20070dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // TODO: if we identified threads to the debugger by their Thread*
20082dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        // rather than their peer's mirror::Object*, we could fix this.
20090dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // Doing so might help us report ZOMBIE threads too.
20100dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        return false;
20110dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      }
2012c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      // Do we want threads from all thread groups?
2013c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      if (desired_thread_group_ == NULL) {
2014c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao        return true;
2015c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      }
20162dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* group = soa_.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(peer);
20170dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      return (group == desired_thread_group_);
20180dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    }
20190dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao
2020dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier    const ScopedObjectAccessUnchecked& soa_;
20212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* const desired_thread_group_;
2022caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    std::vector<JDWP::ObjectId>& thread_ids_;
2023a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
2024a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
202500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
20262dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
202700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ThreadListVisitor tlv(soa, thread_group, thread_ids);
202850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2029f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughes  Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv);
2030a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes}
2031a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
2032caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughesvoid Dbg::GetChildThreadGroups(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& child_thread_group_ids) {
203300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
20342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
2035caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes
2036caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Get the ArrayList<ThreadGroup> "groups" out of this thread group...
2037ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* groups_field = thread_group->GetClass()->FindInstanceField("groups", "Ljava/util/List;");
20382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* groups_array_list = groups_field->GetObject(thread_group);
2039872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2040caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Get the array and size out of the ArrayList<ThreadGroup>...
2041ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* array_field = groups_array_list->GetClass()->FindInstanceField("array", "[Ljava/lang/Object;");
2042ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* size_field = groups_array_list->GetClass()->FindInstanceField("size", "I");
20432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::ObjectArray<mirror::Object>* groups_array =
20442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>();
2045caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  const int32_t size = size_field->GetInt(groups_array_list);
2046caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes
2047caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Copy the first 'size' elements out of the array into the result.
2048caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  for (int32_t i = 0; i < size; ++i) {
2049caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    child_thread_group_ids.push_back(gRegistry->Add(groups_array->Get(i)));
2050caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  }
2051872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2052872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
205300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic int GetStackDepth(Thread* thread)
2054b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct CountStackDepthVisitor : public StackVisitor {
205693ba893c20532990a430741e0a97212900094e8cBrian Carlstrom    explicit CountStackDepthVisitor(Thread* thread)
20577a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth(0) {}
20580399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
205964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
206064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
206164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
20620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!GetMethod()->IsRuntimeMethod()) {
2063f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes        ++depth;
2064f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes      }
2065530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
2066a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
2067a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    size_t depth;
2068a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  };
206908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
20707a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  CountStackDepthVisitor visitor(thread);
20710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2072a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return visitor.depth;
2073872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2074872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2075221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result) {
207600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
2077a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2078221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2079221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2080221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2081221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2082221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2083f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
2084f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
2085f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
2086221229cb523f849f165fdafbf9785010963715daElliott Hughes  result = GetStackDepth(thread);
2087221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
20888696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
20898696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2090306057fd278d75bf3794bd5243a3b6652c487d18Ian RogersJDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame,
2091306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers                                     size_t frame_count, JDWP::ExpandBuf* buf) {
20926e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  class GetFrameVisitor : public StackVisitor {
20936e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   public:
20947a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    GetFrameVisitor(Thread* thread, size_t start_frame, size_t frame_count, JDWP::ExpandBuf* buf)
2095b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
20967a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth_(0),
20976e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes          start_frame_(start_frame), frame_count_(frame_count), buf_(buf) {
20986e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      expandBufAdd4BE(buf_, frame_count_);
209903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
21000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
210100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
210200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
210300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
21040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetMethod()->IsRuntimeMethod()) {
21057934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        return true;  // The debugger can't do anything useful with a frame that has no Method*.
210603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
21076e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_ + frame_count_) {
2108530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes        return false;
210903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
21106e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_) {
21116e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::FrameId frame_id(GetFrameId());
21126e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::JdwpLocation location;
21136e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        SetLocation(location, GetMethod(), GetDexPc());
2114ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers        VLOG(jdwp) << StringPrintf("    Frame %3zd: id=%3" PRIu64 " ", depth_, frame_id) << location;
21156e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAdd8BE(buf_, frame_id);
21166e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAddLocation(buf_, location);
21176e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      }
21186e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      ++depth_;
2119530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
212003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
21216e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes
21226e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   private:
21236e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    size_t depth_;
21246e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t start_frame_;
21256e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t frame_count_;
21266e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    JDWP::ExpandBuf* buf_;
212703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
212800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
212900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2130a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2131221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2132221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2133221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2134221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
2135221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2136f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
2137f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
2138f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
21397a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetFrameVisitor visitor(thread, start_frame, frame_count, buf);
21400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
21416e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
2142872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2143872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2144872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetThreadSelfId() {
2145dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier  ScopedObjectAccessUnchecked soa(Thread::Current());
2146cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  return gRegistry->Add(soa.Self()->GetPeer());
2147872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2148872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2149475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::SuspendVM() {
215000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
2151872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2152872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2153872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ResumeVM() {
2154c61a267e98ed1038b74c33c7740414ced4a27d89Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
2155872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2156872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2157221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) {
215800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedLocalRef<jobject> peer(Thread::Current()->GetJniEnv(), NULL);
215900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
216000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
21612dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id)));
216200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
216300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (peer.get() == NULL) {
216400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
216500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
216600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Suspend thread to build stack trace.
2167f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  bool timed_out;
2168d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  Thread* thread = ThreadList::SuspendThreadByPeer(peer.get(), request_suspension, true,
2169d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                                   &timed_out);
217000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (thread != NULL) {
217100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_NONE;
2172f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else if (timed_out) {
217300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_INTERNAL;
217400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  } else {
217500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
21764e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
2177872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2178872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2179221229cb523f849f165fdafbf9785010963715daElliott Hughesvoid Dbg::ResumeThread(JDWP::ObjectId thread_id) {
218000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
21812dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id);
2182a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  Thread* thread;
2183a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  {
2184a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2185a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    thread = Thread::FromManagedThread(soa, peer);
2186a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  }
21874e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  if (thread == NULL) {
21884e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    LOG(WARNING) << "No such thread for resume: " << peer;
21894e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    return;
21904e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
219100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool needs_resume;
219200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
219350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
219400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    needs_resume = thread->GetSuspendCount() > 0;
219500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
219600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (needs_resume) {
2197546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    Runtime::Current()->GetThreadList()->Resume(thread, true);
2198546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
2199872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2200872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2201872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::SuspendSelf() {
2202475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
2203872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2204872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
22050399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct GetThisVisitor : public StackVisitor {
22067a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
2207b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
22087a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, context), this_object(NULL), frame_id(frame_id) {}
220968fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
221000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
221100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
221200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
22136e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    if (frame_id != GetFrameId()) {
22140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return true;  // continue
22150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    } else {
221662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      this_object = GetThisObject();
221762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return false;
22180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
221968fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  }
2220dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
22212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* this_object;
22226e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  JDWP::FrameId frame_id;
22230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers};
2224ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
222500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersJDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id,
222600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ObjectId* result) {
222700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
222800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* thread;
222900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
223050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2231221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2232221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
2233221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
223400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
22359e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    if (!IsSuspendedForDebugger(soa, thread)) {
223600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      return JDWP::ERR_THREAD_NOT_SUSPENDED;
223700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
22386e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  }
2239caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  UniquePtr<Context> context(Context::Create());
22407a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor visitor(thread, context.get(), frame_id);
22410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
22426e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  *result = gRegistry->Add(visitor.this_object);
22436e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
22440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
22450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
2246cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien HertzJDWP::JdwpError Dbg::GetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot,
2247cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                   JDWP::JdwpTag tag, uint8_t* buf, size_t width) {
22480399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct GetLocalVisitor : public StackVisitor {
22499837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    GetLocalVisitor(const ScopedObjectAccessUnchecked& soa, Thread* thread, Context* context,
22509837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers                    JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint8_t* buf, size_t width)
2251b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
22529837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers        : StackVisitor(thread, context), soa_(soa), frame_id_(frame_id), slot_(slot), tag_(tag),
2253cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz          buf_(buf), width_(width), error_(JDWP::ERR_NONE) {}
2254ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
225500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
225600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
225700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
22580399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetFrameId() != frame_id_) {
22590399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        return true;  // Not our frame, carry on.
22600399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
22610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // TODO: check that the tag is compatible with the actual type of the slot!
2262cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      // TODO: check slot is valid for this method or return INVALID_SLOT error.
2263ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* m = GetMethod();
2264cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      if (m->IsNative()) {
2265cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        // We can't read local value from native method.
2266cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        error_ = JDWP::ERR_OPAQUE_FRAME;
2267cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        return false;
2268cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      }
22690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      uint16_t reg = DemangleSlot(slot_, m);
22700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
22710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      switch (tag_) {
22720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_BOOLEAN:
22730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
22740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
22752bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
22760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get boolean local " << reg << " = " << intVal;
22770399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set1(buf_+1, intVal != 0);
22780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
22790399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
22800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_BYTE:
22810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
22820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
22832bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
22840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get byte local " << reg << " = " << intVal;
22850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set1(buf_+1, intVal);
22860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
22870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
22880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_SHORT:
22890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CHAR:
22900399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
22910399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 2U);
22922bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
22930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get short/char local " << reg << " = " << intVal;
22940399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set2BE(buf_+1, intVal);
22950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
22960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
22970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_INT:
22982bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        {
22992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 4U);
23002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
23012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          VLOG(jdwp) << "get int local " << reg << " = " << intVal;
23022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          JDWP::Set4BE(buf_+1, intVal);
23032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
23042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        break;
23050399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_FLOAT:
23060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 4U);
23082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kFloatVReg);
23090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get int/float local " << reg << " = " << intVal;
23100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set4BE(buf_+1, intVal);
23110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23120399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_ARRAY:
23140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
23162dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
23170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get array local " << reg << " = " << o;
2318590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier          if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
23190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            LOG(FATAL) << "Register " << reg << " expected to hold array: " << o;
23200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
23210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
23220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CLASS_LOADER:
23250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CLASS_OBJECT:
23260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_OBJECT:
23270399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_STRING:
23280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_THREAD:
23290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_THREAD_GROUP:
23300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
23322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
23330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get object local " << reg << " = " << o;
2334590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier          if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
23350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            LOG(FATAL) << "Register " << reg << " expected to hold object: " << o;
23360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
23379837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers          tag_ = TagFromObject(soa_, o);
23380399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
23390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_DOUBLE:
23422bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        {
23432bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 8U);
23442bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t lo = GetVReg(m, reg, kDoubleLoVReg);
23452bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t hi = GetVReg(m, reg + 1, kDoubleHiVReg);
23462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t longVal = (hi << 32) | lo;
23472bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
23482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          JDWP::Set8BE(buf_+1, longVal);
23492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
23502bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        break;
23510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_LONG:
23520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
23530399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 8U);
23542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t lo = GetVReg(m, reg, kLongLoVReg);
23552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t hi = GetVReg(m, reg + 1, kLongHiVReg);
23560399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          uint64_t longVal = (hi << 32) | lo;
23570399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
23580399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set8BE(buf_+1, longVal);
23590399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
23600399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      default:
23620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        LOG(FATAL) << "Unknown tag " << tag_;
23630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
23640399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
2365ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
23660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // Prepend tag, which may have been updated.
23670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      JDWP::Set1(buf_, tag_);
23680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return false;
23690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
23709837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    const ScopedObjectAccessUnchecked& soa_;
23710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::FrameId frame_id_;
23720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const int slot_;
23730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    JDWP::JdwpTag tag_;
23740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    uint8_t* const buf_;
23750399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const size_t width_;
2376cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    JDWP::JdwpError error_;
23770399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
237800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
237900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2380a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2381221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2382221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2383221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2384cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return error;
2385221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2386cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  // TODO check thread is suspended by the debugger ?
23870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  UniquePtr<Context> context(Context::Create());
23889837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers  GetLocalVisitor visitor(soa, thread, context.get(), frame_id, slot, tag, buf, width);
23890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2390cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  return visitor.error_;
23910399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
23920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
2393cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien HertzJDWP::JdwpError Dbg::SetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot,
2394cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz                                   JDWP::JdwpTag tag, uint64_t value, size_t width) {
23950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SetLocalVisitor : public StackVisitor {
23967a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    SetLocalVisitor(Thread* thread, Context* context,
23970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                    JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint64_t value,
2398ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers                    size_t width)
2399b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
24007a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context),
2401cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz          frame_id_(frame_id), slot_(slot), tag_(tag), value_(value), width_(width),
2402cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz          error_(JDWP::ERR_NONE) {}
2403ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
240400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
240500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
240600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
24070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetFrameId() != frame_id_) {
24080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        return true;  // Not our frame, carry on.
2409ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes      }
24100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // TODO: check that the tag is compatible with the actual type of the slot!
2411cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      // TODO: check slot is valid for this method or return INVALID_SLOT error.
2412ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* m = GetMethod();
2413cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      if (m->IsNative()) {
2414cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        // We can't read local value from native method.
2415cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        error_ = JDWP::ERR_OPAQUE_FRAME;
2416cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz        return false;
2417cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz      }
24180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      uint16_t reg = DemangleSlot(slot_, m);
24190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
24200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      switch (tag_) {
24210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_BOOLEAN:
24220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_BYTE:
24230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
24242bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
24250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_SHORT:
24270399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_CHAR:
24280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 2U);
24292bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
24300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_INT:
24322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 4U);
24332bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
24342bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          break;
24350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_FLOAT:
24360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 4U);
24372bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kFloatVReg);
24380399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_ARRAY:
24400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_OBJECT:
24410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_STRING:
24420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
24430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
24442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value_));
244564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes          if (o == ObjectRegistry::kInvalidObject) {
24460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            UNIMPLEMENTED(FATAL) << "return an error code when given an invalid object to store";
24470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
24482bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), kReferenceVReg);
24490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
24500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
24510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_DOUBLE:
24522bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 8U);
24532bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kDoubleLoVReg);
24542bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kDoubleHiVReg);
24552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          break;
24560399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_LONG:
24570399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 8U);
24582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kLongLoVReg);
24592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kLongHiVReg);
24600399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        default:
24620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          LOG(FATAL) << "Unknown tag " << tag_;
24630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
24640399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
24650399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return false;
2466cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    }
24670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
24680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::FrameId frame_id_;
24690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const int slot_;
24700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::JdwpTag tag_;
24710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const uint64_t value_;
24720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const size_t width_;
2473cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    JDWP::JdwpError error_;
24740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
247500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
247600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2477a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2478221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2479221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2480221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2481cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz    return error;
2482221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
2483cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  // TODO check thread is suspended by the debugger ?
248408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  UniquePtr<Context> context(Context::Create());
24857a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  SetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, value, width);
24860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2487cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  return visitor.error_;
2488872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2489872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
24903f52eafe5577b8489f90dc8ed5981b3455206147Sebastien HertzJDWP::ObjectId Dbg::GetThisObjectIdForEvent(mirror::Object* this_object) {
24913f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // If 'this_object' isn't already in the registry, we know that we're not looking for it, so
24923f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // there's no point adding it to the registry and burning through ids.
24933f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // When registering an event request with an instance filter, we've been given an existing object
24943f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // id so it must already be present in the registry when the event fires.
24953f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = 0;
24963f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (this_object != nullptr && gRegistry->Contains(this_object)) {
24973f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    this_id = gRegistry->Add(this_object);
24983f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
24993f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  return this_id;
25003f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
25013f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
2502ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogersvoid Dbg::PostLocationEvent(mirror::ArtMethod* m, int dex_pc, mirror::Object* this_object,
2503579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                            int event_flags, const JValue* return_value) {
25043f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
25053f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
25063f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
25073f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
25083f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK_EQ(m->IsStatic(), this_object == nullptr);
250991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  JDWP::JdwpLocation location;
2510cb19ebf7609f74b223bd86c94f721498795f9bbaSebastien Hertz  SetLocation(location, m, dex_pc);
251191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
25123f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // We need 'this' for InstanceOnly filters only.
25133f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = GetThisObjectIdForEvent(this_object);
2514579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  gJdwpState->PostLocationEvent(&location, this_id, event_flags, return_value);
2515872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2516872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
25173f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostFieldAccessEvent(mirror::ArtMethod* m, int dex_pc,
25183f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                               mirror::Object* this_object, mirror::ArtField* f) {
25193f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
25203f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
25213f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
25223f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
25233f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(f != nullptr);
25243f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::JdwpLocation location;
25253f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  SetLocation(location, m, dex_pc);
25263f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25273f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::RefTypeId type_id = gRegistry->AddRefType(f->GetDeclaringClass());
25283f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::FieldId field_id = ToFieldId(f);
25293f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = gRegistry->Add(this_object);
25303f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25313f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  gJdwpState->PostFieldEvent(&location, type_id, field_id, this_id, nullptr, false);
25323f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
25333f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25343f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostFieldModificationEvent(mirror::ArtMethod* m, int dex_pc,
25353f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                     mirror::Object* this_object, mirror::ArtField* f,
25363f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz                                     const JValue* field_value) {
25373f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  if (!IsDebuggerActive()) {
25383f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz    return;
25393f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  }
25403f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(m != nullptr);
25413f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(f != nullptr);
25423f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  DCHECK(field_value != nullptr);
25433f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::JdwpLocation location;
25443f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  SetLocation(location, m, dex_pc);
25453f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25463f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::RefTypeId type_id = gRegistry->AddRefType(f->GetDeclaringClass());
25473f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::FieldId field_id = ToFieldId(f);
25483f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = gRegistry->Add(this_object);
25493f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25503f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  gJdwpState->PostFieldEvent(&location, type_id, field_id, this_id, field_value, true);
25513f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz}
25523f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz
25533f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertzvoid Dbg::PostException(const ThrowLocation& throw_location,
2554ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                        mirror::ArtMethod* catch_method,
255564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes                        uint32_t catch_dex_pc, mirror::Throwable* exception_object) {
2556c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
25570ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers    return;
25580ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers  }
25594740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
256062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  JDWP::JdwpLocation jdwp_throw_location;
256162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SetLocation(jdwp_throw_location, throw_location.GetMethod(), throw_location.GetDexPc());
2562d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  JDWP::JdwpLocation catch_location;
2563caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  SetLocation(catch_location, catch_method, catch_dex_pc);
2564d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
25653f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  // We need 'this' for InstanceOnly filters only.
25663f52eafe5577b8489f90dc8ed5981b3455206147Sebastien Hertz  JDWP::ObjectId this_id = GetThisObjectIdForEvent(throw_location.GetThis());
256764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::ObjectId exception_id = gRegistry->Add(exception_object);
256864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId exception_class_id = gRegistry->AddRefType(exception_object->GetClass());
2569d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
257062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  gJdwpState->PostException(&jdwp_throw_location, exception_id, exception_class_id, &catch_location,
257162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                            this_id);
2572872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2573872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
25742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::PostClassPrepare(mirror::Class* c) {
2575c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
25764740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes    return;
25774740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  }
25784740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
25793d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes  // OLD-TODO - we currently always send both "verified" and "prepared" since
25804740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  // debuggers seem to like that.  There might be some advantage to honesty,
25814740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  // since the class may not yet be verified.
25824740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
25834d8fd49509fdcf203107fb33c62d8f451b6eb1d0Sebastien Hertz  JDWP::JdwpTypeTag tag = GetTypeTag(c);
2584fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan Rogers  gJdwpState->PostClassPrepare(tag, gRegistry->Add(c),
2585dfb325e0ddd746cd8f7c2e3723b3a573eb7cc111Ian Rogers                               ClassHelper(c).GetDescriptor(), state);
2586872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2587872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
258862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object,
2589ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers                         mirror::ArtMethod* m, uint32_t dex_pc) {
259062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) {
25912aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes    return;
259291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
259391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
25942aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes  int event_flags = 0;
25952aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes
25968696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  if (IsBreakpoint(m, dex_pc)) {
25978696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    event_flags |= kBreakpoint;
259891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
259991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
260061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // If the debugger is single-stepping one of our threads, check to
260161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // see if we're that thread and we've reached a step point.
260261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  const SingleStepControl* single_step_control = thread->GetSingleStepControl();
260361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
260461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (single_step_control->is_active) {
260561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    CHECK(!m->IsNative());
260661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    if (single_step_control->step_depth == JDWP::SD_INTO) {
260761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step into method calls.  We break when the line number
260861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // or method pointer changes.  If we're in SS_MIN mode, we
260961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // always stop.
261061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (single_step_control->method != m) {
261161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
261261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new method";
261361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (single_step_control->step_size == JDWP::SS_MIN) {
261461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
261561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new instruction";
2616bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      } else if (single_step_control->ContainsDexPc(dex_pc)) {
261761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
261861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new line";
261961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
262061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else if (single_step_control->step_depth == JDWP::SD_OVER) {
262161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step over method calls.  We break when the line number is
262261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // different and the frame depth is <= the original frame
262361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth.  (We can't just compare on the method, because we
262461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // might get unrolled past it by an exception, and it's tricky
262561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // to identify recursion.)
262661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
262761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
262861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
262961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
263061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Popped up one or more frames, always trigger.
263161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
263261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
263361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (stack_depth == single_step_control->stack_depth) {
263461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Same depth, see if we moved.
263561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control->step_size == JDWP::SS_MIN) {
26368696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          event_flags |= kSingleStep;
26378696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          VLOG(jdwp) << "SS new instruction";
2638bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz        } else if (single_step_control->ContainsDexPc(dex_pc)) {
26392435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          event_flags |= kSingleStep;
26402435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          VLOG(jdwp) << "SS new line";
264191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes        }
264261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
264361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else {
264461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      CHECK_EQ(single_step_control->step_depth, JDWP::SD_OUT);
264561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Return from the current method.  We break when the frame
264661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth pops up.
264761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
264861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // This differs from the "method exit" break in that it stops
264961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // with the PC at the next instruction in the returned-to
265061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // function, rather than the end of the returning function.
265161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
265261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
265361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
265461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
265561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
26568696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
265791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    }
265891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
265991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
266091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // If there's something interesting going on, see if it matches one
266191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // of the debugger filters.
266291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (event_flags != 0) {
2663579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, nullptr);
266491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
266591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes}
266691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
26674d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz// Process request while all mutator threads are suspended.
26684d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::ProcessDeoptimizationRequest(const DeoptimizationRequest& request) {
2669138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
26704d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  switch (request.kind) {
26714d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kNothing:
26724d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      LOG(WARNING) << "Ignoring empty deoptimization request.";
26734d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
26744d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullDeoptimization:
26757ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize the world ...";
26764d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->DeoptimizeEverything();
26777ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize the world DONE";
26784d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
26794d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullUndeoptimization:
26807ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize the world ...";
26814d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->UndeoptimizeEverything();
26827ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize the world DONE";
26834d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
26844d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveDeoptimization:
26857ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.method) << " ...";
26864d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->Deoptimize(request.method);
26877ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Deoptimize method " << PrettyMethod(request.method) << " DONE";
26884d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
26894d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveUndeoptimization:
26907ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.method) << " ...";
26914d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      instrumentation->Undeoptimize(request.method);
26927ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Undeoptimize method " << PrettyMethod(request.method) << " DONE";
26934d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
26944d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    default:
26954d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      LOG(FATAL) << "Unsupported deoptimization request kind " << request.kind;
26964d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
26974d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
26984d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz}
26994d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz
27007ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::DelayFullUndeoptimization() {
27017ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  MutexLock mu(Thread::Current(), *deoptimization_lock_);
27027ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  ++delayed_full_undeoptimization_count_;
27037ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  DCHECK_LE(delayed_full_undeoptimization_count_, full_deoptimization_event_count_);
27047ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
27057ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
27067ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::ProcessDelayedFullUndeoptimizations() {
27077ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  // TODO: avoid taking the lock twice (once here and once in ManageDeoptimization).
27087ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  {
27097ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    MutexLock mu(Thread::Current(), *deoptimization_lock_);
27107ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    while (delayed_full_undeoptimization_count_ > 0) {
27117ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      DeoptimizationRequest req;
27127ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      req.kind = DeoptimizationRequest::kFullUndeoptimization;
27137ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      req.method = nullptr;
27147ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      RequestDeoptimizationLocked(req);
27157ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      --delayed_full_undeoptimization_count_;
27167ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    }
27177ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  }
27187ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  ManageDeoptimization();
27197ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
27207ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
27214d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::RequestDeoptimization(const DeoptimizationRequest& req) {
27224d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  if (req.kind == DeoptimizationRequest::kNothing) {
27234d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    // Nothing to do.
27244d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    return;
27254d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
27264d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  MutexLock mu(Thread::Current(), *deoptimization_lock_);
27277ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz  RequestDeoptimizationLocked(req);
27287ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz}
27297ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz
27307ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertzvoid Dbg::RequestDeoptimizationLocked(const DeoptimizationRequest& req) {
27314d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  switch (req.kind) {
27324d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullDeoptimization: {
27334d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK(req.method == nullptr);
27344d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      if (full_deoptimization_event_count_ == 0) {
27357ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz        VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
27367ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                   << " for full deoptimization";
27374d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz        deoptimization_requests_.push_back(req);
2738138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      }
27394d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      ++full_deoptimization_event_count_;
27404d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27414d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
27424d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kFullUndeoptimization: {
27434d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK(req.method == nullptr);
27444d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK_GT(full_deoptimization_event_count_, 0U);
27454d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      --full_deoptimization_event_count_;
27464d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      if (full_deoptimization_event_count_ == 0) {
27477ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz        VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
27487ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                   << " for full undeoptimization";
27494d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz        deoptimization_requests_.push_back(req);
2750138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      }
27514d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27524d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
27534d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveDeoptimization: {
27544d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK(req.method != nullptr);
27557ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
27567ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                 << " for deoptimization of " << PrettyMethod(req.method);
27574d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.push_back(req);
27584d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27594d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
27604d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    case DeoptimizationRequest::kSelectiveUndeoptimization: {
27614d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      DCHECK(req.method != nullptr);
27627ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Queue request #" << deoptimization_requests_.size()
27637ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz                 << " for undeoptimization of " << PrettyMethod(req.method);
27644d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      deoptimization_requests_.push_back(req);
27654d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
27664d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
27674d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    default: {
27684d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      LOG(FATAL) << "Unknown deoptimization request kind " << req.kind;
27694d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      break;
2770138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2771138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2772138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz}
2773138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2774138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertzvoid Dbg::ManageDeoptimization() {
2775138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  Thread* const self = Thread::Current();
2776138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  {
2777138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    // Avoid suspend/resume if there is no pending request.
27784d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(self, *deoptimization_lock_);
27794d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    if (deoptimization_requests_.empty()) {
2780138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz      return;
2781138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2782138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2783138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  CHECK_EQ(self->GetState(), kRunnable);
2784138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  self->TransitionFromRunnableToSuspended(kWaitingForDeoptimization);
2785138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  // We need to suspend mutator threads first.
2786138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  Runtime* const runtime = Runtime::Current();
2787138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetThreadList()->SuspendAll();
2788138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  const ThreadState old_state = self->SetStateUnsafe(kRunnable);
27894d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  {
27904d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    MutexLock mu(self, *deoptimization_lock_);
27917ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz    size_t req_index = 0;
27924d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    for (const DeoptimizationRequest& request : deoptimization_requests_) {
27937ec2f1ca3cbd021848da75d5566f7239ce29676fSebastien Hertz      VLOG(jdwp) << "Process deoptimization request #" << req_index++;
27944d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz      ProcessDeoptimizationRequest(request);
27954d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    }
27964d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz    deoptimization_requests_.clear();
27974d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertz  }
2798138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
2799138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  runtime->GetThreadList()->ResumeAll();
2800138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  self->TransitionFromSuspendedToRunnable();
2801138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz}
2802138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2803a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic bool IsMethodPossiblyInlined(Thread* self, mirror::ArtMethod* m)
2804a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2805a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  MethodHelper mh(m);
2806a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const DexFile::CodeItem* code_item = mh.GetCodeItem();
2807a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (code_item == nullptr) {
2808a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // TODO We should not be asked to watch location in a native or abstract method so the code item
2809a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // should never be null. We could just check we never encounter this case.
2810a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    return false;
2811a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  }
2812a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
2813a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
2814a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  verifier::MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader,
2815a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz                                    &mh.GetClassDef(), code_item, m->GetDexMethodIndex(), m,
2816a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz                                    m->GetAccessFlags(), false, true);
2817a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  // Note: we don't need to verify the method.
2818a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  return InlineMethodAnalyser::AnalyseMethodCode(&verifier, nullptr);
2819a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
2820138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2821a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic const Breakpoint* FindFirstBreakpointForMethod(mirror::ArtMethod* m)
2822a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_) {
2823a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  for (const Breakpoint& breakpoint : gBreakpoints) {
2824a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (breakpoint.method == m) {
2825a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      return &breakpoint;
2826138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2827a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  }
2828a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  return nullptr;
2829a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
2830138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2831a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Sanity checks all existing breakpoints on the same method.
2832a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzstatic void SanityCheckExistingBreakpoints(mirror::ArtMethod* m, bool need_full_deoptimization)
2833a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_)  {
2834a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (kIsDebugBuild) {
2835a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    for (const Breakpoint& breakpoint : gBreakpoints) {
2836a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK_EQ(need_full_deoptimization, breakpoint.need_full_deoptimization);
2837a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
2838a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
2839a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // We should have deoptimized everything but not "selectively" deoptimized this method.
2840a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK(Runtime::Current()->GetInstrumentation()->AreAllMethodsDeoptimized());
2841a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK(!Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
2842a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
2843a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // We should have "selectively" deoptimized this method.
2844a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // Note: while we have not deoptimized everything for this method, we may have done it for
2845a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // another event.
2846a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      CHECK(Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
2847a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
2848138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2849a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz}
2850138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2851a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Installs a breakpoint at the specified location. Also indicates through the deoptimization
2852a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// request if we need to deoptimize.
2853a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertzvoid Dbg::WatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
2854a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  Thread* const self = Thread::Current();
2855a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  mirror::ArtMethod* m = FromMethodId(location->method_id);
2856a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  DCHECK(m != nullptr) << "No method for method id " << location->method_id;
2857a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
2858a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  MutexLock mu(self, *Locks::breakpoint_lock_);
2859a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m);
2860a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  bool need_full_deoptimization;
2861a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (existing_breakpoint == nullptr) {
2862a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is no breakpoint on this method yet: we need to deoptimize. If this method may be
2863a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // inlined, we deoptimize everything; otherwise we deoptimize only this method.
2864a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    need_full_deoptimization = IsMethodPossiblyInlined(self, m);
2865a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
2866a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->kind = DeoptimizationRequest::kFullDeoptimization;
2867a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->method = nullptr;
2868a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
2869a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->kind = DeoptimizationRequest::kSelectiveDeoptimization;
2870a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->method = m;
2871a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
2872a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  } else {
2873a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is at least one breakpoint for this method: we don't need to deoptimize.
2874a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    req->kind = DeoptimizationRequest::kNothing;
2875a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    req->method = nullptr;
2876a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
2877a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    need_full_deoptimization = existing_breakpoint->need_full_deoptimization;
2878a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SanityCheckExistingBreakpoints(m, need_full_deoptimization);
2879138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2880a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz
2881a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  gBreakpoints.push_back(Breakpoint(m, location->dex_pc, need_full_deoptimization));
2882a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": "
2883a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz             << gBreakpoints[gBreakpoints.size() - 1];
2884872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2885872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2886a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// Uninstalls a breakpoint at the specified location. Also indicates through the deoptimization
2887a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz// request if we need to undeoptimize.
28884d25df3f76f864b7629ac8c0046d46997f293d8dSebastien Hertzvoid Dbg::UnwatchLocation(const JDWP::JdwpLocation* location, DeoptimizationRequest* req) {
2889ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(location->method_id);
2890a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  DCHECK(m != nullptr) << "No method for method id " << location->method_id;
2891138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz
2892a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
2893a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  bool need_full_deoptimization = false;
2894a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  for (size_t i = 0, e = gBreakpoints.size(); i < e; ++i) {
2895a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == location->dex_pc) {
2896a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
2897a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      need_full_deoptimization = gBreakpoints[i].need_full_deoptimization;
2898a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      DCHECK_NE(need_full_deoptimization, Runtime::Current()->GetInstrumentation()->IsDeoptimized(m));
2899a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      gBreakpoints.erase(gBreakpoints.begin() + i);
2900a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      break;
2901138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz    }
2902138dbfc3336e379d74d157086f69a0fbe830089bSebastien Hertz  }
2903a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  const Breakpoint* const existing_breakpoint = FindFirstBreakpointForMethod(m);
2904a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  if (existing_breakpoint == nullptr) {
2905a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is no more breakpoint on this method: we need to undeoptimize.
2906a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    if (need_full_deoptimization) {
2907a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // This method required full deoptimization: we need to undeoptimize everything.
2908a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->kind = DeoptimizationRequest::kFullUndeoptimization;
2909a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->method = nullptr;
2910a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    } else {
2911a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      // This method required selective deoptimization: we need to undeoptimize only that method.
2912a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->kind = DeoptimizationRequest::kSelectiveUndeoptimization;
2913a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz      req->method = m;
2914a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    }
2915a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz  } else {
2916a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    // There is at least one breakpoint for this method: we don't need to undeoptimize.
2917a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    req->kind = DeoptimizationRequest::kNothing;
2918a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    req->method = nullptr;
2919a76a6d4cb3dedec909bd927f20bc0ab23198a337Sebastien Hertz    SanityCheckExistingBreakpoints(m, need_full_deoptimization);
29208696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
2921872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2922872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2923449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't
2924449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// cause suspension if the thread is the current thread.
2925449db33fafa29578df60e8a323f78d5eb6247e76Jeff Haoclass ScopedThreadSuspension {
2926449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao public:
292733e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers  ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id)
292852d131d8e71ae9ea915ea84a3f95d49547e8e661Sebastien Hertz      LOCKS_EXCLUDED(Locks::thread_list_lock_)
292933e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
2930449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      thread_(NULL),
2931449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_(JDWP::ERR_NONE),
2932449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      self_suspend_(false),
293333e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      other_suspend_(false) {
2934449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    ScopedObjectAccessUnchecked soa(self);
2935449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    {
2936449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2937449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_ = DecodeThread(soa, thread_id, thread_);
2938449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
2939449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (error_ == JDWP::ERR_NONE) {
2940449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      if (thread_ == soa.Self()) {
2941449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        self_suspend_ = true;
2942449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      } else {
2943449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
2944449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        jobject thread_peer = gRegistry->GetJObject(thread_id);
2945449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        bool timed_out;
2946d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        Thread* suspended_thread = ThreadList::SuspendThreadByPeer(thread_peer, true, true,
2947d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                                                   &timed_out);
2948449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension);
2949449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        if (suspended_thread == NULL) {
2950449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          // Thread terminated from under us while suspending.
2951449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          error_ = JDWP::ERR_INVALID_THREAD;
2952449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        } else {
2953449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          CHECK_EQ(suspended_thread, thread_);
2954449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          other_suspend_ = true;
2955449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        }
2956449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      }
2957449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
2958449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2959449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2960449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* GetThread() const {
2961449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return thread_;
2962449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2963449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2964449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError GetError() const {
2965449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return error_;
2966449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2967449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2968449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ~ScopedThreadSuspension() {
2969449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (other_suspend_) {
2970449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      Runtime::Current()->GetThreadList()->Resume(thread_, true);
2971449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
2972449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2973449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2974449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao private:
2975449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* thread_;
2976449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError error_;
2977449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool self_suspend_;
2978449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool other_suspend_;
2979449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao};
2980449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2981221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size,
298200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::JdwpStepDepth step_depth) {
2983449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* self = Thread::Current();
2984449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ScopedThreadSuspension sts(self, thread_id);
2985449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  if (sts.GetError() != JDWP::ERR_NONE) {
2986449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return sts.GetError();
29872435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
29888696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
29892435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
29902435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Work out what Method* we're in, the current line number, and how deep the stack currently
29912435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // is for step-out.
29922435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
29932435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
29940399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SingleStepStackVisitor : public StackVisitor {
299561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    explicit SingleStepStackVisitor(Thread* thread, SingleStepControl* single_step_control,
299661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz                                    int32_t* line_number)
2997b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
299861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        : StackVisitor(thread, NULL), single_step_control_(single_step_control),
299961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          line_number_(line_number) {
300061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      DCHECK_EQ(single_step_control_, thread->GetSingleStepControl());
300161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->method = NULL;
300261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->stack_depth = 0;
30038696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
3004ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
300500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
300600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
300700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
300861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      mirror::ArtMethod* m = GetMethod();
30090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!m->IsRuntimeMethod()) {
301061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        ++single_step_control_->stack_depth;
301161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control_->method == NULL) {
3012ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers          mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache();
301361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->method = m;
301461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          *line_number_ = -1;
30152435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          if (dex_cache != NULL) {
30164445a7e3398a6143939168097a3aa275b734504dIan Rogers            const DexFile& dex_file = *dex_cache->GetDexFile();
301761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz            *line_number_ = dex_file.GetLineNumFromPC(m, GetDexPc());
30182435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          }
30198696433d1b3d8ba15288483b777edd888de69135Elliott Hughes        }
30208696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
3021530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
30228696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
302361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
302461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
302561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    int32_t* const line_number_;
30268696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  };
3027449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
302861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* const thread = sts.GetThread();
302961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepControl* const single_step_control = thread->GetSingleStepControl();
303061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
303161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  int32_t line_number = -1;
303261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepStackVisitor visitor(thread, single_step_control, &line_number);
30330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
30348696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
30352435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
30362435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Find the dex_pc values that correspond to the current line, for line-based single-stepping.
30372435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
30382435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
30392435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  struct DebugCallbackContext {
3040bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    explicit DebugCallbackContext(SingleStepControl* single_step_control, int32_t line_number,
3041bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz                                  const DexFile::CodeItem* code_item)
3042bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      : single_step_control_(single_step_control), line_number_(line_number), code_item_(code_item),
304361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        last_pc_valid(false), last_pc(0) {
30442435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
30452435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
304661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) {
30472435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context);
304861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (static_cast<int32_t>(line_number) == context->line_number_) {
30492435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        if (!context->last_pc_valid) {
30502435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          // Everything from this address until the next line change is ours.
30512435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc = address;
30522435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc_valid = true;
30532435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
30542435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Otherwise, if we're already in a valid range for this line,
30552435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // just keep going (shouldn't really happen)...
30567934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      } else if (context->last_pc_valid) {  // and the line number is new
30572435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Add everything from the last entry up until here to the set
30582435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) {
305961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          context->single_step_control_->dex_pcs.insert(dex_pc);
30602435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
30612435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        context->last_pc_valid = false;
30622435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
30637934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return false;  // There may be multiple entries for any given line.
30642435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
30652435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
306661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    ~DebugCallbackContext() {
30672435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      // If the line number was the last in the position table...
30682435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      if (last_pc_valid) {
3069bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz        size_t end = code_item_->insns_size_in_code_units_;
30702435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) {
307161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->dex_pcs.insert(dex_pc);
30722435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
30732435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
30742435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
30752435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
307661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
307761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    const int32_t line_number_;
3078bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    const DexFile::CodeItem* const code_item_;
30792435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    bool last_pc_valid;
30802435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    uint32_t last_pc;
30812435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  };
308261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->dex_pcs.clear();
3083ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  mirror::ArtMethod* m = single_step_control->method;
308461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (!m->IsNative()) {
30853e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes    MethodHelper mh(m);
3086bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    const DexFile::CodeItem* const code_item = mh.GetCodeItem();
3087bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    DebugCallbackContext context(single_step_control, line_number, code_item);
3088bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(),
30893e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes                                    DebugCallbackContext::Callback, NULL, &context);
30903e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes  }
30912435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
30922435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
30932435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Everything else...
30942435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
30952435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
309661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_size = step_size;
309761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_depth = step_depth;
309861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->is_active = true;
30998696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
31002435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  if (VLOG_IS_ON(jdwp)) {
310161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step thread: " << *thread;
310261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step size: " << single_step_control->step_size;
310361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step depth: " << single_step_control->step_depth;
310461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->method);
310561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current line: " << line_number;
310661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->stack_depth;
31072435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step dex_pc values:";
3108bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    for (uint32_t dex_pc : single_step_control->dex_pcs) {
3109bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz      VLOG(jdwp) << StringPrintf(" %#x", dex_pc);
31102435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
31112435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
31122435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
31132435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
3114872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3115872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
311661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertzvoid Dbg::UnconfigureStep(JDWP::ObjectId thread_id) {
311761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
311861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
311961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* thread;
312061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
312187118ed6f3f99e7df33214c277cf200a7b9a7499Sebastien Hertz  if (error == JDWP::ERR_NONE) {
312261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* single_step_control = thread->GetSingleStepControl();
312361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    DCHECK(single_step_control != nullptr);
3124bb43b433c845e253a11346f0f03706d58a44f23cSebastien Hertz    single_step_control->Clear();
312561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  }
3126872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3127872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
312845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughesstatic char JdwpTagToShortyChar(JDWP::JdwpTag tag) {
312945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  switch (tag) {
313045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    default:
313145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag);
313245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
313345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Primitives.
313445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BYTE:    return 'B';
313545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CHAR:    return 'C';
313645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_FLOAT:   return 'F';
313745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_DOUBLE:  return 'D';
313845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_INT:     return 'I';
313945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_LONG:    return 'J';
314045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_SHORT:   return 'S';
314145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_VOID:    return 'V';
314245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BOOLEAN: return 'Z';
314345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
314445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Reference types.
314545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_ARRAY:
314645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_OBJECT:
314745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_STRING:
314845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD:
314945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD_GROUP:
315045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_LOADER:
315145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_OBJECT:
315245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return 'L';
315345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  }
315445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes}
315545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
315688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id,
315788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                  JDWP::RefTypeId class_id, JDWP::MethodId method_id,
315800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  uint32_t arg_count, uint64_t* arg_values,
315900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* arg_types, uint32_t options,
316000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* pResultTag, uint64_t* pResultValue,
316100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::ObjectId* pExceptionId) {
3162d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  ThreadList* thread_list = Runtime::Current()->GetThreadList();
3163d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3164d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  Thread* targetThread = NULL;
3165d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  DebugInvokeReq* req = NULL;
316600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* self = Thread::Current();
3167d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
316800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(self);
316950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
3170221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, targetThread);
3171221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
3172221229cb523f849f165fdafbf9785010963715daElliott Hughes      LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id;
3173221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
3174d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3175d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    req = targetThread->GetInvokeReq();
3176d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (!req->ready) {
3177d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread;
3178d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      return JDWP::ERR_INVALID_THREAD;
3179d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3180d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3181d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3182d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We currently have a bug where we don't successfully resume the
3183d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * target thread if the suspend count is too deep.  We're expected to
3184d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * require one "resume" for each "suspend", but when asked to execute
3185d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a method we have to resume fully and then re-suspend it back to the
3186d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * same level.  (The easiest way to cause this is to type "suspend"
3187d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * multiple times in jdb.)
3188d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
3189d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * It's unclear what this means when the event specifies "resume all"
3190d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * and some threads are suspended more deeply than others.  This is
3191d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a rare problem, so for now we just prevent it from hanging forever
3192d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * by rejecting the method invocation request.  Without this, we will
3193d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * be stuck waiting on a suspended thread.
3194d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
319500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    int suspend_count;
319600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
319750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
319800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      suspend_count = targetThread->GetSuspendCount();
319900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
3200d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (suspend_count > 1) {
3201d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count;
32027934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return JDWP::ERR_THREAD_SUSPENDED;  // Probably not expected here.
3203d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3204d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
32053f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    JDWP::JdwpError status;
32062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id);
320764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (receiver == ObjectRegistry::kInvalidObject) {
32083f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
32093f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
321045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
32112dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* thread = gRegistry->Get<mirror::Object*>(thread_id);
321264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (thread == ObjectRegistry::kInvalidObject) {
32133f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
32143f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
321545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // TODO: check that 'thread' is actually a java.lang.Thread!
321645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
32172dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_id, status);
321845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (c == NULL) {
32193f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return status;
32203f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
322145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
3222ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = FromMethodId(method_id);
322345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic() != (receiver == NULL)) {
322445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_INVALID_METHODID;
322545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
322645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic()) {
322745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (m->GetDeclaringClass() != c) {
322845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
322945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
323045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    } else {
323145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
323245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
323345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
323445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
323545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
323645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Check the argument list matches the method.
323745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    MethodHelper mh(m);
323845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (mh.GetShortyLength() - 1 != arg_count) {
323945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_ILLEGAL_ARGUMENT;
324045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
324145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    const char* shorty = mh.GetShorty();
32420920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes    const DexFile::TypeList* types = mh.GetParameterTypeList();
324345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    for (size_t i = 0; i < arg_count; ++i) {
324445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) {
324545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_ILLEGAL_ARGUMENT;
324645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
32470920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
32480920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      if (shorty[i + 1] == 'L') {
32490920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        // Did we really get an argument of an appropriate reference type?
32500920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        mirror::Class* parameter_type = mh.GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
32510920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i]);
32520920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        if (argument == ObjectRegistry::kInvalidObject) {
32530920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_INVALID_OBJECT;
32540920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
32550630ab5239a7d7be24dedbc3f66c822332446fc3Sebastien Hertz        if (argument != NULL && !argument->InstanceOf(parameter_type)) {
32560920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_ILLEGAL_ARGUMENT;
32570920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
32580920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
32590920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        // Turn the on-the-wire ObjectId into a jobject.
32600920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]);
32610920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        v.l = gRegistry->GetJObject(arg_values[i]);
32620920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      }
326345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
326445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
3265d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->receiver = receiver;
3266d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->thread = thread;
3267d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->klass = c;
3268d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->method = m;
3269d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_count = arg_count;
3270d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_values = arg_values;
3271d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->options = options;
3272d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->invoke_needed = true;
3273d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3274d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3275d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // The fact that we've released the thread list lock is a bit risky --- if the thread goes
3276d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // away we're sitting high and dry -- but we must release this before the ResumeAllThreads
3277d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // call, and it's unwise to hold it during WaitForSuspend.
3278d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3279d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
3280d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3281d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We change our (JDWP thread) status, which should be THREAD_RUNNING,
328281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes     * so we can suspend for a GC if the invoke request causes us to
3283d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * run out of memory.  It's also a good idea to change it before locking
3284d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the invokeReq mutex, although that should never be held for long.
3285d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
328600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
3287d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
32884dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Transferring control to event thread";
3289d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    {
3290d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      MutexLock mu(self, req->lock);
3291d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3292d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
32934dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming all threads";
329400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers        thread_list->UndoDebuggerSuspensions();
3295d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      } else {
32964dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming event thread only";
3297d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes        thread_list->Resume(targetThread, true);
3298d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
3299d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3300d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      // Wait for the request to finish executing.
3301d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      while (req->invoke_needed) {
3302d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz        req->cond.Wait(self);
3303d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
3304d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
33054dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Control has returned from event thread";
3306d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3307d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* wait for thread to re-suspend itself */
3308df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom    SuspendThread(thread_id, false /* request_suspension */);
330900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
3310d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3311d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3312d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  /*
3313d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * Suspend the threads.  We waited for the target thread to suspend
3314d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * itself, so all we need to do is suspend the others.
3315d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   *
3316d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * The suspendAllThreads() call will double-suspend the event thread,
3317d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * so we want to resume the target thread once to keep the books straight.
3318d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   */
3319d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
332000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
33214dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Suspending all threads";
332200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    thread_list->SuspendAllForDebugger();
332300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
33244dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Resuming event thread to balance the count";
3325d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    thread_list->Resume(targetThread, true);
3326d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3327d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3328d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Copy the result.
3329d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pResultTag = req->result_tag;
3330d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (IsPrimitiveTag(req->result_tag)) {
3331f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = req->result_value.GetJ();
3332d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else {
3333f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = gRegistry->Add(req->result_value.GetL());
3334d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3335d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pExceptionId = req->exception;
3336d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  return req->error;
3337872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3338872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3339872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
334000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
3341d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
334281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // We can be called while an exception is pending. We need
3343d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // to preserve that across the method invocation.
334462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SirtRef<mirror::Object> old_throw_this_object(soa.Self(), NULL);
3345ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), NULL);
334662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SirtRef<mirror::Throwable> old_exception(soa.Self(), NULL);
334762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  uint32_t old_throw_dex_pc;
334862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  {
334962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation old_throw_location;
335062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
335162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_this_object.reset(old_throw_location.GetThis());
335262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_method.reset(old_throw_location.GetMethod());
335362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_exception.reset(old_exception_obj);
335462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_dex_pc = old_throw_location.GetDexPc();
335562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->ClearException();
335662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
3357d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3358d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Translate the method through the vtable, unless the debugger wants to suppress it.
3359c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier  SirtRef<mirror::ArtMethod> m(soa.Self(), pReq->method);
3360d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz  if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver != NULL) {
336183a47d8a950867da833dfb5c3d8ddfb3d87a8cfeSebastien Hertz    mirror::ArtMethod* actual_method = pReq->klass->FindVirtualMethodForVirtualOrInterface(m.get());
3362c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier    if (actual_method != m.get()) {
3363c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier      VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m.get()) << " to " << PrettyMethod(actual_method);
3364c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier      m.reset(actual_method);
336545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
3366d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3367c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier  VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m.get())
3368d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " receiver=" << pReq->receiver
3369d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " arg_count=" << pReq->arg_count;
3370c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier  CHECK(m.get() != nullptr);
3371d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3372d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  CHECK_EQ(sizeof(jvalue), sizeof(uint64_t));
3373d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
337483a47d8a950867da833dfb5c3d8ddfb3d87a8cfeSebastien Hertz  pReq->result_value = InvokeWithJValues(soa, pReq->receiver, soa.EncodeMethod(m.get()),
337553b8b09fc80329539585dcf43657bc5f4ecefdffIan Rogers                                         reinterpret_cast<jvalue*>(pReq->arg_values));
3376d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
337762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  mirror::Throwable* exception = soa.Self()->GetException(NULL);
337862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->ClearException();
337962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  pReq->exception = gRegistry->Add(exception);
3380c528dba35b5faece51ca658fc008b688f8b690adMathieu Chartier  pReq->result_tag = BasicTagFromDescriptor(MethodHelper(m.get()).GetShorty());
3381d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (pReq->exception != 0) {
338262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    VLOG(jdwp) << "  JDWP invocation returning with exception=" << exception
338362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        << " " << exception->Dump();
3384f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    pReq->result_value.SetJ(0);
3385d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else if (pReq->result_tag == JDWP::JT_OBJECT) {
3386d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* if no exception thrown, examine object result more closely */
33879837939678bb5dcba178e5fb00ed59b5d14c8d9bIan Rogers    JDWP::JdwpTag new_tag = TagFromObject(soa, pReq->result_value.GetL());
3388d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (new_tag != pReq->result_tag) {
33894dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes      VLOG(jdwp) << "  JDWP promoted result from " << pReq->result_tag << " to " << new_tag;
3390d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      pReq->result_tag = new_tag;
3391d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
3392d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3393d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
3394d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * Register the object.  We don't actually need an ObjectId yet,
3395d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * but we do need to be sure that the GC won't move or discard the
3396d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * object when we switch out of RUNNING.  The ObjectId conversion
3397d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * will add the object to the "do not touch" list.
3398d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
3399d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We can't use the "tracked allocation" mechanism here because
3400d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the object is going to be handed off to a different thread.
3401d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
3402f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    gRegistry->Add(pReq->result_value.GetL());
3403d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3404d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
3405d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (old_exception.get() != NULL) {
340662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
340762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                         old_throw_dex_pc);
340862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
3409d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
3410872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3411872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3412d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes/*
34134b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * "request" contains a full JDWP packet, possibly with multiple chunks.  We
3414f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * need to process each, accumulate the replies, and ship the whole thing
3415f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * back.
3416f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
3417f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * Returns "true" if we have a reply.  The reply buffer is newly allocated,
3418f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * and includes the chunk type/length, followed by the data.
3419f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
34203d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes * OLD-TODO: we currently assume that the request and reply include a single
3421f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * chunk.  If this becomes inconvenient we will need to adapt.
3422f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes */
34234b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesbool Dbg::DdmHandlePacket(JDWP::Request& request, uint8_t** pReplyBuf, int* pReplyLen) {
3424f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  Thread* self = Thread::Current();
3425f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  JNIEnv* env = self->GetJniEnv();
3426f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
34274b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t type = request.ReadUnsigned32("type");
34284b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t length = request.ReadUnsigned32("length");
34294b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
34304b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  // Create a byte[] corresponding to 'request'.
34314b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  size_t request_length = request.size();
34324b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length));
34336a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (dataArray.get() == NULL) {
34344b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    LOG(WARNING) << "byte[] allocation failed: " << request_length;
3435f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
3436f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3437f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
34384b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  env->SetByteArrayRegion(dataArray.get(), 0, request_length, reinterpret_cast<const jbyte*>(request.data()));
34394b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  request.Skip(request_length);
3440f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3441f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Run through and find all chunks.  [Currently just find the first.]
34426a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ScopedByteArrayRO contents(env, dataArray.get());
34434b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (length != request_length) {
3444ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%zd)", length, request_length);
3445f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3446f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3447f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3448f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
3449eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
3450eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                                                                 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
34514b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                                                 type, dataArray.get(), 0, length));
3452f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (env->ExceptionCheck()) {
3453f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
3454f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionDescribe();
3455f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
3456f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3457f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3458f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
34596a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (chunk.get() == NULL) {
3460f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3461f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3462f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3463f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  /*
3464f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * Pull the pieces out of the chunk.  We copy the results into a
3465f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * newly-allocated buffer that the caller can free.  We don't want to
3466f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * continue using the Chunk object because nothing has a reference to it.
3467f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
3468f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * We could avoid this by returning type/data/offset/length and having
3469f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * the caller be aware of the object lifetime issues, but that
347081ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes   * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
3471f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * if we have responses for multiple chunks.
3472f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
3473f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * So we're pretty much stuck with copying data around multiple times.
3474f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   */
3475eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
34764b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
3477eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
3478eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
3479f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
34804dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length);
34816a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (length == 0 || replyData.get() == NULL) {
3482f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3483f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3484f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
34854b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  const int kChunkHdrLen = 8;
3486f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  uint8_t* reply = new uint8_t[length + kChunkHdrLen];
3487f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (reply == NULL) {
3488f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
3489f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
3490f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
3491f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 0, type);
3492f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 4, length);
34936a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
3494f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
3495f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyBuf = reply;
3496f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyLen = length + kChunkHdrLen;
3497f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
34984b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length);
3499f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  return true;
3500872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3501872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3502a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::DdmBroadcast(bool connect) {
35034dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
350447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
350547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  Thread* self = Thread::Current();
350650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  if (self->GetState() != kRunnable) {
350750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
350850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    /* try anyway? */
350947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
351047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
351147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  JNIEnv* env = self->GetJniEnv();
351247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
3513eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
3514eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
3515eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            event);
351647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (env->ExceptionCheck()) {
351747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
351847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionDescribe();
351947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionClear();
352047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
352147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
352247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3523872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmConnected() {
3524a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(true);
3525872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3526872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3527872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmDisconnected() {
3528a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(false);
352947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = false;
353047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
353147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
353247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes/*
35338218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes * Send a notification when a thread starts, stops, or changes its name.
353447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes *
353547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * Because we broadcast the full set of threads when the notifications are
353647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * first enabled, it's possible for "thread" to be actively executing.
353747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes */
35388218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
353947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (!gDdmThreadNotification) {
354047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    return;
354147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
354247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
35438218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  if (type == CHUNK_TYPE("THDE")) {
35448218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    uint8_t buf[4];
3545d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Set4BE(&buf[0], t->GetThreadId());
35468218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
35478218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  } else {
35488218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
354900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
35502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    SirtRef<mirror::String> name(soa.Self(), t->GetThreadName(soa));
355147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    size_t char_count = (name.get() != NULL) ? name->GetLength() : 0;
3552725a957985171d712d5c048cc3d00ff14968784bjeffhao    const jchar* chars = (name.get() != NULL) ? name->GetCharArray()->GetData() : NULL;
355347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
355421f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    std::vector<uint8_t> bytes;
3555d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Append4BE(bytes, t->GetThreadId());
3556545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    JDWP::AppendUtf16BE(bytes, chars, char_count);
355721f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
355821f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    Dbg::DdmSendChunk(type, bytes);
355947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
356047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
356147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
356247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::DdmSetThreadNotification(bool enable) {
356300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Enable/disable thread notifications.
356447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = enable;
356547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (enable) {
356600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Suspend the VM then post thread start notifications for all threads. Threads attaching will
356700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // see a suspension in progress and block until that ends. They then post their own start
356800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // notification.
356900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    SuspendVM();
357000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    std::list<Thread*> threads;
357150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
357200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
357350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu(self, *Locks::thread_list_lock_);
357400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      threads = Runtime::Current()->GetThreadList()->GetList();
357500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
357600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
357750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      ScopedObjectAccess soa(self);
357802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (Thread* thread : threads) {
357902e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR"));
358000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
358100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
358200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ResumeVM();
358347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
358447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
358547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3586a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
3587c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (IsDebuggerActive()) {
3588dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier    ScopedObjectAccessUnchecked soa(Thread::Current());
3589cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers    JDWP::ObjectId id = gRegistry->Add(t->GetPeer());
35908218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR"));
359147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
35928218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  Dbg::DdmSendThreadNotification(t, type);
359347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
359447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
359547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadStart(Thread* t) {
3596a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
359747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
359847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
359947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadDeath(Thread* t) {
3600a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
3601872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3602872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
36038218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) {
36043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(buf != NULL);
36053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  iovec vec[1];
36063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
36073bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_len = byte_count;
36083bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Dbg::DdmSendChunkV(type, vec, 1);
3609872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3610872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
361121f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) {
361221f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  DdmSendChunk(type, bytes.size(), &bytes[0]);
361321f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes}
361421f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes
3615f52935278fca8c7aa220543eef4544e3d1105d91Brian Carlstromvoid Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
36163bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (gJdwpState == NULL) {
36174dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type;
36183bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
3619cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    gJdwpState->DdmSendChunkV(type, iov, iov_count);
36203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
3621872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3622872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3623767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesint Dbg::DdmHandleHpifChunk(HpifWhen when) {
3624767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when == HPIF_WHEN_NOW) {
36257162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(when);
3626767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return true;
3627767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3628767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3629767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
3630767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
3631767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3632767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3633767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3634767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  gDdmHpifWhen = when;
3635767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
3636767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3637767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3638767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesbool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
3639767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
3640767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
3641767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3642767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3643767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3644767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
3645767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
3646767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3647767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3648767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3649767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (native) {
3650767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhen = when;
3651767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhat = what;
3652767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  } else {
3653767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhen = when;
3654767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhat = what;
3655767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3656767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
3657767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3658767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
36597162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesvoid Dbg::DdmSendHeapInfo(HpifWhen reason) {
36607162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  // If there's a one-shot 'when', reset it.
36617162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  if (reason == gDdmHpifWhen) {
36627162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
36637162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes      gDdmHpifWhen = HPIF_WHEN_NEVER;
36647162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    }
36657162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  }
36667162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes
36677162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  /*
36687162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Chunk HPIF (client --> server)
36697162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
36707162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Heap Info. General information about the heap,
36717162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * suitable for a summary display.
36727162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
36737162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   [u4]: number of heaps
36747162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
36757162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   For each heap:
36767162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: heap ID
36777162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u8]: timestamp in ms since Unix epoch
36787162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u1]: capture reason (same as 'when' value from server)
36797162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: max heap size in bytes (-Xmx)
36807162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current heap size in bytes
36817162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of bytes allocated
36827162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of objects allocated
36837162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   */
36847162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  uint8_t heap_count = 1;
36851d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::Heap* heap = Runtime::Current()->GetHeap();
368621f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  std::vector<uint8_t> bytes;
3687545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append4BE(bytes, heap_count);
36887934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, 1);  // Heap id (bogus; we only have one heap).
3689545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append8BE(bytes, MilliTime());
3690545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append1BE(bytes, reason);
36917934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetMaxMemory());  // Max allowed heap size in bytes.
36927934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetTotalMemory());  // Current heap size in bytes.
3693b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetBytesAllocated());
3694b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetObjectsAllocated());
369521f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
369621f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes);
3697767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3698767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
36996a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgSolidity {
37006a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FREE = 0,
37016a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_HARD = 1,
37026a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SOFT = 2,
37036a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_WEAK = 3,
37046a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_PHANTOM = 4,
37056a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FINALIZABLE = 5,
37066a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SWEEP = 6,
37076a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
37086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgKind {
37106a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_OBJECT = 0,
37116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_CLASS_OBJECT = 1,
37126a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_1 = 2,
37136a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_2 = 3,
37146a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_4 = 4,
37156a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_8 = 5,
37166a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_UNKNOWN = 6,
37176a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_NATIVE = 7,
37186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
37196a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37206a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_PARTIAL (1<<7)
37216a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
37226a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
372330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogersclass HeapChunkContext {
372430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers public:
37256a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Maximum chunk size.  Obtain this from the formula:
37266a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
37276a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  HeapChunkContext(bool merge, bool native)
372830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      : buf_(16384 - 16),
372930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        type_(0),
373030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        merge_(merge) {
37316a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
37326a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    if (native) {
373330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = CHUNK_TYPE("NHSG");
37346a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    } else {
373530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
37366a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
37376a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
37386a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37396a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ~HeapChunkContext() {
374030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (p_ > &buf_[0]) {
37416a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      Flush();
37426a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
37436a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
37446a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37456a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void EnsureHeader(const void* chunk_ptr) {
374630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (!needHeader_) {
37476a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      return;
37486a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
37496a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37506a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Start a new HPSx chunk.
37517934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 1);  // Heap id (bogus; we only have one heap).
37527934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write1BE(&p_, 8);  // Size of allocation unit, in bytes.
37536a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
37547934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr));  // virtual address of segment start.
37557934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 0);  // offset of this piece (relative to the virtual address).
37566a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // [u4]: length of piece, in allocation units
37576a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // We won't know this until we're done, so save the offset and stuff in a dummy value.
375830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = p_;
375930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Write4BE(&p_, 0x55555555);
376030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = false;
37616a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
37626a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3763b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3764d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    if (pieceLenField_ == NULL) {
3765d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
3766d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      CHECK(needHeader_);
3767d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      return;
3768d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    }
37696a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Patch the "length of piece" field.
377030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(&buf_[0], pieceLenField_);
377130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(pieceLenField_, p_);
377230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Set4BE(pieceLenField_, totalAllocationUnits_);
37736a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
377430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]);
37756a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
37766a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
37776a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
377800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg)
3779b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3780b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
378130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes);
3782a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
3783a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
37846a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes private:
3785a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  enum { ALLOCATION_UNIT_SIZE = 8 };
3786a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
37876a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void Reset() {
378830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    p_ = &buf_[0];
378915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    startOfNextMemoryChunk_ = NULL;
379030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    totalAllocationUnits_ = 0;
379130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = true;
379230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = NULL;
37936a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
37946a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
379500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void HeapChunkCallback(void* start, void* /*end*/, size_t used_bytes)
3796b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3797b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
379830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
379930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // in the following code not to allocate memory, by ensuring buf_ is of the correct size
380015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (used_bytes == 0) {
380115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start == NULL) {
380215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            // Reset for start of new heap.
380315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
380415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
380515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
380615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Only process in use memory so that free region information
380715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // also includes dlmalloc book keeping.
3808a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
3809a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
38106a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
381115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    /* If we're looking at the native heap, we'll just return
381215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     * (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks
381315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     */
381415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bool native = type_ == CHUNK_TYPE("NHSG");
381515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
381615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (startOfNextMemoryChunk_ != NULL) {
381715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Transmit any pending free memory. Native free memory of
381815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // over kMaxFreeLen could be because of the use of mmaps, so
381915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // don't report. If not free memory then start a new segment.
382015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        bool flush = true;
382115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start > startOfNextMemoryChunk_) {
382215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            const size_t kMaxFreeLen = 2 * kPageSize;
382315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeStart = startOfNextMemoryChunk_;
382415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeEnd = start;
38252d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom            size_t freeLen = reinterpret_cast<char*>(freeEnd) - reinterpret_cast<char*>(freeStart);
382615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            if (!native || freeLen < kMaxFreeLen) {
382715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), freeStart, freeLen);
382815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                flush = false;
382915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            }
383015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
383115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (flush) {
383215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
383315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
383415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
383515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
3836ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers    mirror::Object* obj = reinterpret_cast<mirror::Object*>(start);
3837a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
3838a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Determine the type of this chunk.
3839a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
3840a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If it's the same, we should combine them.
384115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    uint8_t state = ExamineObject(obj, native);
384215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
384315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // allocation then the first sizeof(size_t) may belong to it.
384415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    const size_t dlMallocOverhead = sizeof(size_t);
384515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    AppendChunk(state, start, used_bytes + dlMallocOverhead);
38462d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom    startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + dlMallocOverhead;
384715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  }
384815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
384915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void AppendChunk(uint8_t state, void* ptr, size_t length)
3850b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
385115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // Make sure there's enough room left in the buffer.
385215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
385315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // 17 bytes for any header.
385415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t needed = (((length/ALLOCATION_UNIT_SIZE + 255) / 256) * 2) + 17;
385515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
385615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
385715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      Flush();
385815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
3859a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
386015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
386115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
386215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
386315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers          << needed << " bytes)";
386415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return;
386515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
386615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    EnsureHeader(ptr);
3867a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Write out the chunk description.
386815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    length /= ALLOCATION_UNIT_SIZE;   // Convert to allocation units.
386915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    totalAllocationUnits_ += length;
387015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    while (length > 256) {
387130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = state | HPSG_PARTIAL;
387230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = 255;     // length - 1
387315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      length -= 256;
3874a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
387530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    *p_++ = state;
387615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    *p_++ = length - 1;
38776a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
38786a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3879ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers  uint8_t ExamineObject(mirror::Object* o, bool is_native_heap)
3880ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
3881a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (o == NULL) {
3882a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_FREE, 0);
3883a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
38846a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3885a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // It's an allocated chunk. Figure out what it is.
38866a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3887a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If we're looking at the native heap, we'll just return
3888a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks.
388900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (is_native_heap) {
3890a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
38916a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
38926a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
38935bfa60ffcc953340feb711ed05cf576ac821905eIan Rogers    if (!Runtime::Current()->GetHeap()->IsLiveObjectLocked(o)) {
389415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
389500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
389600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
38972dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = o->GetClass();
3898a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c == NULL) {
3899a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      // The object was probably just created but hasn't been initialized yet.
3900a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
3901a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
39026a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3903590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(c)) {
390415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
3905a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
3906a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
39076a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3908a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsClassClass()) {
3909a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
39106a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
39116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3912a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsArrayClass()) {
3913a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (o->IsObjectArray()) {
3914a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3915a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
3916a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      switch (c->GetComponentSize()) {
3917a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
3918a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
3919a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3920a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
3921a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
39226a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
39236a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3924a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
39256a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
39266a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
392730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  std::vector<uint8_t> buf_;
392830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* p_;
392930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* pieceLenField_;
393015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void* startOfNextMemoryChunk_;
393130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  size_t totalAllocationUnits_;
393230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint32_t type_;
393330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool merge_;
393430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool needHeader_;
393530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers
3936a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
3937a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes};
39386a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39396a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesvoid Dbg::DdmSendHeapSegments(bool native) {
39406a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhen when;
39416a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhat what;
39426a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (!native) {
39436a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmHpsgWhen;
39446a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmHpsgWhat;
39456a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  } else {
39466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmNhsgWhen;
39476a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmNhsgWhat;
39486a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
39496a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (when == HPSG_WHEN_NEVER) {
39506a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    return;
39516a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
39526a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39536a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Figure out what kind of chunks we'll be sending.
39546a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what);
39556a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
39566a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // First, send a heap start chunk.
39576a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  uint8_t heap_id[4];
39587934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Set4BE(&heap_id[0], 1);  // Heap id (bogus; we only have one heap).
39596a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id);
39606a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3961cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Thread* self = Thread::Current();
3962cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
3963cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // To allow the Walk/InspectAll() below to exclusively-lock the
3964cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // mutator lock, temporarily release the shared access to the
3965cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // mutator lock here by transitioning to the suspended state.
3966cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Locks::mutator_lock_->AssertSharedHeld(self);
3967cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  self->TransitionFromRunnableToSuspended(kSuspended);
3968cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
39696a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Send a series of heap segment chunks.
3970a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native);
3971a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (native) {
39721d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context);
3973a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
39741d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
39751d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
39761d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
39771d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    for (It cur = spaces.begin(), end = spaces.end(); cur != end; ++cur) {
3978cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      if ((*cur)->IsMallocSpace()) {
3979cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi        (*cur)->AsMallocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
3980b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier      }
3981b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
3982e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    // Walk the large objects, these are not in the AllocSpace.
3983e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
3984a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
39856a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3986cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Shared-lock the mutator lock back.
3987cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  self->TransitionFromSuspendedToRunnable();
3988cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Locks::mutator_lock_->AssertSharedHeld(self);
3989cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
39906a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Finally, send a heap end chunk.
39916a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id);
3992767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3993767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3994b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughesstatic size_t GetAllocTrackerMax() {
3995b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#ifdef HAVE_ANDROID_OS
3996b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  // Check whether there's a system property overriding the number of records.
3997b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  const char* propertyName = "dalvik.vm.allocTrackerMax";
3998b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  char allocRecordMaxString[PROPERTY_VALUE_MAX];
3999b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  if (property_get(propertyName, allocRecordMaxString, "") > 0) {
4000b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    char* end;
4001b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    size_t value = strtoul(allocRecordMaxString, &end, 10);
4002b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (*end != '\0') {
40033e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
40043e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- invalid";
4005b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
4006b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
4007b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (!IsPowerOfTwo(value)) {
40083e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
40093e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- not power of two";
4010b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
4011b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
4012b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    return value;
4013b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  }
4014b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#endif
4015b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  return kDefaultNumAllocRecords;
4016b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes}
4017b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes
4018545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::SetAllocTrackingEnabled(bool enabled) {
4019545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (enabled) {
4020b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    {
4021b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
4022b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      if (recent_allocation_records_ == NULL) {
4023b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        alloc_record_max_ = GetAllocTrackerMax();
4024b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        LOG(INFO) << "Enabling alloc tracker (" << alloc_record_max_ << " entries of "
4025b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz            << kMaxAllocRecordStackDepth << " frames, taking "
4026b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz            << PrettySize(sizeof(AllocRecord) * alloc_record_max_) << ")";
4027b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        alloc_record_head_ = alloc_record_count_ = 0;
4028b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        recent_allocation_records_ = new AllocRecord[alloc_record_max_];
4029b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz        CHECK(recent_allocation_records_ != NULL);
4030b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      }
4031545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4032fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers    Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints();
4033545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  } else {
4034fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers    Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
4035b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    {
4036b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
4037b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      delete[] recent_allocation_records_;
4038b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz      recent_allocation_records_ = NULL;
4039b98063a4d54c5a741152a1eebc262d8e223b3b25Sebastien Hertz    }
4040545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4041545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4042545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
40430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct AllocRecordStackVisitor : public StackVisitor {
40447a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor(Thread* thread, AllocRecord* record)
4045b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
40467a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, NULL), record(record), depth(0) {}
4047545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
404800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
404900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
405000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
4051545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if (depth >= kMaxAllocRecordStackDepth) {
4052530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return false;
4053545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4054ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = GetMethod();
40550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    if (!m->IsRuntimeMethod()) {
40560399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].method = m;
40570399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].dex_pc = GetDexPc();
4058530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      ++depth;
4059545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4060530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes    return true;
4061545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4062545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4063545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  ~AllocRecordStackVisitor() {
4064545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // Clear out any unused stack trace elements.
4065545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (; depth < kMaxAllocRecordStackDepth; ++depth) {
4066545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      record->stack[depth].method = NULL;
40670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].dex_pc = 0;
4068545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4069545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4070545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4071545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  AllocRecord* record;
4072545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t depth;
4073545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
4074545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
40752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::RecordAllocation(mirror::Class* type, size_t byte_count) {
4076545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  Thread* self = Thread::Current();
4077545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  CHECK(self != NULL);
4078545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4079719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  MutexLock mu(self, *alloc_tracker_lock_);
4080545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
4081545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
4082545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4083545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4084545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Advance and clip.
4085719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (++alloc_record_head_ == alloc_record_max_) {
4086719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    alloc_record_head_ = 0;
4087545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4088545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4089545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the basics.
4090719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  AllocRecord* record = &recent_allocation_records_[alloc_record_head_];
4091545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  record->type = type;
4092545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  record->byte_count = byte_count;
4093d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  record->thin_lock_id = self->GetThreadId();
4094545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4095545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the stack trace.
40967a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor visitor(self, record);
40970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
4098545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4099719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (alloc_record_count_ < alloc_record_max_) {
4100719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    ++alloc_record_count_;
4101545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4102545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4103545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4104a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// Returns the index of the head element.
4105a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
4106a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We point at the most-recently-written record, so if gAllocRecordCount is 1
4107a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// we want to use the current element.  Take "head+1" and subtract count
4108a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// from it.
4109a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
4110a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We need to handle underflow in our circular buffer, so we add
4111b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes// gAllocRecordMax and then mask it back down.
4112719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogerssize_t Dbg::HeadIndex() {
4113719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  return (Dbg::alloc_record_head_ + 1 + Dbg::alloc_record_max_ - Dbg::alloc_record_count_) &
4114719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      (Dbg::alloc_record_max_ - 1);
4115545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4116545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4117545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::DumpRecentAllocations() {
411800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
4119719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  MutexLock mu(soa.Self(), *alloc_tracker_lock_);
4120545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
4121545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    LOG(INFO) << "Not recording tracked allocations";
4122545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
4123545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4124545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4125545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // "i" is the head of the list.  We want to start at the end of the
4126545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // list and move forward to the tail.
4127a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t i = HeadIndex();
4128719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  size_t count = alloc_record_count_;
4129545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4130719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  LOG(INFO) << "Tracked allocations, (head=" << alloc_record_head_ << " count=" << count << ")";
4131545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  while (count--) {
4132545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    AllocRecord* record = &recent_allocation_records_[i];
4133545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4134a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->thin_lock_id, record->byte_count)
4135545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes              << PrettyClass(record->type);
4136545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4137545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
4138ef7d42fca18c16fbaf103822ad16f23246e2905dIan Rogers      mirror::ArtMethod* m = record->stack[stack_frame].method;
4139545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      if (m == NULL) {
4140545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes        break;
4141545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
4142545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      LOG(INFO) << "    " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber();
4143545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4144545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4145545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // pause periodically to help logcat catch up
4146545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if ((count % 5) == 0) {
4147545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      usleep(40000);
4148545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4149545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4150719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    i = (i + 1) & (alloc_record_max_ - 1);
4151545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4152545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4153545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
41543b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartiervoid Dbg::UpdateObjectPointers(IsMarkedCallback* callback, void* arg) {
4155719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers  if (recent_allocation_records_ != nullptr) {
4156719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    MutexLock mu(Thread::Current(), *alloc_tracker_lock_);
4157719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    size_t i = HeadIndex();
4158719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    size_t count = alloc_record_count_;
4159719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    while (count--) {
4160719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      AllocRecord* record = &recent_allocation_records_[i];
4161719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      DCHECK(record != nullptr);
41623b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier      record->UpdateObjectPointers(callback, arg);
4163719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      i = (i + 1) & (alloc_record_max_ - 1);
4164412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    }
4165412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
4166412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (gRegistry != nullptr) {
41673b05e9ba874449dbff65b01b8781001f7d93eea6Mathieu Chartier    gRegistry->UpdateObjectPointers(callback, arg);
4168412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
4169412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier}
4170412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
4171412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartiervoid Dbg::AllowNewObjectRegistryObjects() {
4172412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (gRegistry != nullptr) {
4173412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    gRegistry->AllowNewObjects();
4174412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
4175412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier}
4176412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
4177412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartiervoid Dbg::DisallowNewObjectRegistryObjects() {
4178412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  if (gRegistry != nullptr) {
4179412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier    gRegistry->DisallowNewObjects();
4180412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier  }
4181412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier}
4182412c7fced915fc8d4d5e4166e977d55c809168a6Mathieu Chartier
4183545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesclass StringTable {
4184545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes public:
4185545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  StringTable() {
4186545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4187545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
41886d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  void Add(const char* s) {
4189545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    table_.insert(s);
4190545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4191545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4192a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t IndexOf(const char* s) const {
419302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    auto it = table_.find(s);
4194a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    if (it == table_.end()) {
4195a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes      LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
4196a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    }
4197a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    return std::distance(table_.begin(), it);
4198545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4199545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4200a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t Size() const {
4201545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return table_.size();
4202545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4203545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4204a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  void WriteTo(std::vector<uint8_t>& bytes) const {
420502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const std::string& str : table_) {
420602e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      const char* s = str.c_str();
42076d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      size_t s_len = CountModifiedUtf8Chars(s);
42086d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      UniquePtr<uint16_t> s_utf16(new uint16_t[s_len]);
42096d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      ConvertModifiedUtf8ToUtf16(s_utf16.get(), s);
42106d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len);
4211545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4212545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4213545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4214545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes private:
4215a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  std::set<std::string> table_;
4216545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(StringTable);
4217545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
4218545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4219280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertzstatic const char* GetMethodSourceFile(MethodHelper* mh)
4220280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
4221280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz  DCHECK(mh != nullptr);
4222280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz  const char* source_file = mh->GetDeclaringClassSourceFile();
4223280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz  return (source_file != nullptr) ? source_file : "";
4224280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz}
4225280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz
4226545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes/*
4227545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The data we send to DDMS contains everything we have recorded.
4228545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4229545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * Message header (all values big-endian):
4230545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) message header len (to allow future expansion); includes itself
4231545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) entry header len
4232545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) stack frame len
4233545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of entries
4234545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (4b) offset to string table from start of message
4235545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of class name strings
4236545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of method name strings
4237545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of source file name strings
4238545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * For each entry:
4239545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (4b) total allocation size
4240221229cb523f849f165fdafbf9785010963715daElliott Hughes *   (2b) thread id
4241545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (2b) allocated object's class name index
4242545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (1b) stack depth
4243545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   For each stack frame:
4244545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method's class name
4245545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method name
4246545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method source file
4247545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) line number, clipped to 32767; -2 if native; -1 if no source
4248545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) class name strings
4249545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) method name strings
4250545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) source file strings
4251545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4252545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * As with other DDM traffic, strings are sent as a 4-byte length
4253545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * followed by UTF-16 data.
4254545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4255545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We send up 16-bit unsigned indexes into string tables.  In theory there
4256b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes * can be (kMaxAllocRecordStackDepth * gAllocRecordMax) unique strings in
4257545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * each table, but in practice there should be far fewer.
4258545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4259545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The chief reason for using a string table here is to keep the size of
4260545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * the DDMS message to a minimum.  This is partly to make the protocol
4261545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * efficient, but also because we have to form the whole thing up all at
4262545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * once in a memory buffer.
4263545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
4264545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We use separate string tables for class names, method names, and source
4265545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * files to keep the indexes small.  There will generally be no overlap
4266545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * between the contents of these tables.
4267545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes */
4268545a064aca775ba801790fced3d713d8a87bfc61Elliott HughesjbyteArray Dbg::GetRecentAllocations() {
4269545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (false) {
4270545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    DumpRecentAllocations();
4271545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4272545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
427350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* self = Thread::Current();
427446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  std::vector<uint8_t> bytes;
427546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  {
4276719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    MutexLock mu(self, *alloc_tracker_lock_);
427746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
427846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 1: generate string tables.
427946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
428046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable class_names;
428146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable method_names;
428246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable filenames;
428346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4284719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    int count = alloc_record_count_;
428546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    int idx = HeadIndex();
428646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
428746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
428846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
428946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      class_names.Add(ClassHelper(record->type).GetDescriptor());
429046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
429146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      MethodHelper mh;
429246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) {
4293ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        mirror::ArtMethod* m = record->stack[i].method;
429446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        if (m != NULL) {
429546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          mh.ChangeMethod(m);
429646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          class_names.Add(mh.GetDeclaringClassDescriptor());
429746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          method_names.Add(mh.GetName());
4298280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz          filenames.Add(GetMethodSourceFile(&mh));
429946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        }
430046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      }
4301545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4302719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      idx = (idx + 1) & (alloc_record_max_ - 1);
430346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    }
430446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4305719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    LOG(INFO) << "allocation records: " << alloc_record_count_;
430646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
430746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
430846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 2: Generate the output and store it in the buffer.
430946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
431046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
431146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) message header len (to allow future expansion); includes itself
431246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) entry header len
431346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) stack frame len
431446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kMessageHeaderLen = 15;
431546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kEntryHeaderLen = 9;
431646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kStackFrameLen = 8;
431746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kMessageHeaderLen);
431846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kEntryHeaderLen);
431946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kStackFrameLen);
432046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
432146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of entries
432246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (4b) offset to string table from start of message
432346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of class name strings
432446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of method name strings
432546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of source file name strings
4326719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    JDWP::Append2BE(bytes, alloc_record_count_);
432746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    size_t string_table_offset = bytes.size();
43287934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Append4BE(bytes, 0);  // We'll patch this later...
432946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, class_names.Size());
433046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, method_names.Size());
433146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, filenames.Size());
433246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4333719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers    count = alloc_record_count_;
433446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    idx = HeadIndex();
433546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
433646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // For each entry:
433746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (4b) total allocation size
433846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) thread id
433946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) allocated object's class name index
434046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (1b) stack depth
434146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
434246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      size_t stack_depth = record->GetDepth();
4343590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      ClassHelper kh(record->type);
434446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      size_t allocated_object_class_name_index = class_names.IndexOf(kh.GetDescriptor());
434546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append4BE(bytes, record->byte_count);
434646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, record->thin_lock_id);
434746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, allocated_object_class_name_index);
434846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append1BE(bytes, stack_depth);
434946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
435046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      MethodHelper mh;
435146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
435246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // For each stack frame:
435346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method's class name
435446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method name
435546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method source file
435646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) line number, clipped to 32767; -2 if native; -1 if no source
435746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        mh.ChangeMethod(record->stack[stack_frame].method);
435846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        size_t class_name_index = class_names.IndexOf(mh.GetDeclaringClassDescriptor());
435946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        size_t method_name_index = method_names.IndexOf(mh.GetName());
4360280286ac8a0e3a30c68be511c8b7a0a4d62936d7Sebastien Hertz        size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(&mh));
436146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, class_name_index);
436246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, method_name_index);
436346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, file_name_index);
436446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber());
4365545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
436646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
4367719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers      idx = (idx + 1) & (alloc_record_max_ - 1);
4368545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
4369545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
437046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) class name strings
437146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) method name strings
437246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) source file strings
437346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Set4BE(&bytes[string_table_offset], bytes.size());
437446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    class_names.WriteTo(bytes);
437546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    method_names.WriteTo(bytes);
437646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    filenames.WriteTo(bytes);
4377545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
437850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  JNIEnv* env = self->GetJniEnv();
4379545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  jbyteArray result = env->NewByteArray(bytes.size());
4380545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (result != NULL) {
4381545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
4382545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
4383545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  return result;
4384545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
4385545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
4386872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}  // namespace art
4387