debugger.cc revision f2910eef247b45ce1d489e323b36b5de6b6157aa
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"
315d9173014c1ca09f7249a6b07629aa37778b5f8fJeff Hao#include "invoke_arg_array_builder.h"
3264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "jdwp/object_registry.h"
33ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_field-inl.h"
34ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.h"
352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class.h"
362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class_loader.h"
382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
402dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/throwable.h"
416d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h"
42a0e180632411f7fe0edf454e571c42209ee7b540Elliott Hughes#include "safe_map.h"
4364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes#include "scoped_thread_state_change.h"
446a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#include "ScopedLocalRef.h"
45f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes#include "ScopedPrimitiveArray.h"
461f5393447b9f45be7918042d9ee7b521376de866Ian Rogers#include "sirt_ref.h"
4747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes#include "stack_indirect_reference_table.h"
48475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes#include "thread_list.h"
4962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers#include "throw_location.h"
502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "utf.h"
51eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "well_known_classes.h"
52475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
533d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#ifdef HAVE_ANDROID_OS
543d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#include "cutils/properties.h"
553d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom#endif
563d92d523089bdd7881d2319414a29bf77172b432Brian Carlstrom
57872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesnamespace art {
58872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
597934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstromstatic const size_t kMaxAllocRecordStackDepth = 16;  // Max 255.
607934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstromstatic const size_t kDefaultNumAllocRecords = 64*1024;  // Must be a power of 2.
61475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
62545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstruct AllocRecordStackTraceElement {
63ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* method;
640399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  uint32_t dex_pc;
65545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
66b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  int32_t LineNumber() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    return MethodHelper(method).GetLineNumFromDexPC(dex_pc);
68545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
69545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
70545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
71545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesstruct AllocRecord {
722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* type;
73545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t byte_count;
74545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  uint16_t thin_lock_id;
757934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  AllocRecordStackTraceElement stack[kMaxAllocRecordStackDepth];  // Unused entries have NULL method.
76545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
77545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t GetDepth() {
78545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    size_t depth = 0;
79545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    while (depth < kMaxAllocRecordStackDepth && stack[depth].method != NULL) {
80545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      ++depth;
81545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
82545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return depth;
83545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
84545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
85545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
868696433d1b3d8ba15288483b777edd888de69135Elliott Hughesstruct Breakpoint {
87ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* method;
88a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  uint32_t dex_pc;
89ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  Breakpoint(mirror::ArtMethod* method, uint32_t dex_pc) : method(method), dex_pc(dex_pc) {}
908696433d1b3d8ba15288483b777edd888de69135Elliott Hughes};
918696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
9200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs)
93b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
94229feb7a09317919ee51c06d1c3e715cea25da75Elliott Hughes  os << StringPrintf("Breakpoint[%s @%#x]", PrettyMethod(rhs.method).c_str(), rhs.dex_pc);
958696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return os;
968696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
978696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
988696433d1b3d8ba15288483b777edd888de69135Elliott Hughesstruct SingleStepControl {
998696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  // Are we single-stepping right now?
1008696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  bool is_active;
1018696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  Thread* thread;
1028696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
1038696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  JDWP::JdwpStepSize step_size;
1048696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  JDWP::JdwpStepDepth step_depth;
1058696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
106ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  const mirror::ArtMethod* method;
1077934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  int32_t line_number;  // Or -1 for native methods.
1082435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  std::set<uint32_t> dex_pcs;
1098696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  int stack_depth;
1108696433d1b3d8ba15288483b777edd888de69135Elliott Hughes};
1118696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
11262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersclass DebugInstrumentationListener : public instrumentation::InstrumentationListener {
11362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers public:
11462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  DebugInstrumentationListener() {}
11562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual ~DebugInstrumentationListener() {}
11662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
11762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
118ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                             const mirror::ArtMethod* method, uint32_t dex_pc)
11962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
12062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
12162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
12262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
12362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
12462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    Dbg::PostLocationEvent(method, 0, this_object, Dbg::kMethodEntry);
12562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
12662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
12762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual void MethodExited(Thread* thread, mirror::Object* this_object,
128ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                            const mirror::ArtMethod* method,
12962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                            uint32_t dex_pc, const JValue& return_value)
13062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
13162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    UNUSED(return_value);
13262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
13362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
13462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
13562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
13662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    Dbg::PostLocationEvent(method, dex_pc, this_object, Dbg::kMethodExit);
13762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
13862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
139ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  virtual void MethodUnwind(Thread* thread, const mirror::ArtMethod* method,
14062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                            uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
14162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    // We're not recorded to listen to this kind of event, so complain.
14262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method)
14362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        << " " << dex_pc;
14462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
14562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
14662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
147ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                          const mirror::ArtMethod* method, uint32_t new_dex_pc)
14862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
14962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc);
15062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
15162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
15262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
153ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                               mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
15462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                               mirror::Throwable* exception_object)
15562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
15662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    Dbg::PostException(thread, throw_location, catch_method, catch_dex_pc, exception_object);
15762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
15862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers} gDebugInstrumentationListener;
15962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1604ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes// JDWP is allowed unless the Zygote forbids it.
1614ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesstatic bool gJdwpAllowed = true;
1624ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
163c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line?
1643bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gJdwpConfigured = false;
1653bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
166c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Broken-down JDWP options. (Only valid if IsJdwpConfigured() is true.)
167376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughesstatic JDWP::JdwpOptions gJdwpOptions;
1683bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
1693bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes// Runtime JDWP state.
1703bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic JDWP::JdwpState* gJdwpState = NULL;
1713bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerConnected;  // debugger or DDMS is connected.
1723bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerActive;     // debugger is making requests.
1738696433d1b3d8ba15288483b777edd888de69135Elliott Hughesstatic bool gDisposed;           // debugger called VirtualMachine.Dispose, so we should drop the connection.
1743bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
17547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesstatic bool gDdmThreadNotification = false;
17647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
177767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes// DDMS GC-related settings.
178767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
179767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
180767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmHpsgWhat;
181767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
182767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmNhsgWhat;
183767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
184475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesstatic ObjectRegistry* gRegistry = NULL;
185475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
186545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes// Recent allocation tracking.
187df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstromstatic Mutex gAllocTrackerLock DEFAULT_MUTEX_ACQUIRED_AFTER("AllocTracker lock");
1887934ac288acfb2552bb0b06ec1f61e5820d924a4Brian CarlstromAllocRecord* Dbg::recent_allocation_records_ PT_GUARDED_BY(gAllocTrackerLock) = NULL;  // TODO: CircularBuffer<AllocRecord>
189b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughesstatic size_t gAllocRecordMax GUARDED_BY(gAllocTrackerLock) = 0;
190f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughesstatic size_t gAllocRecordHead GUARDED_BY(gAllocTrackerLock) = 0;
191f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughesstatic size_t gAllocRecordCount GUARDED_BY(gAllocTrackerLock) = 0;
192545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
1938696433d1b3d8ba15288483b777edd888de69135Elliott Hughes// Breakpoints and single-stepping.
19409bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhaostatic std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_);
19509bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhaostatic SingleStepControl gSingleStepControl GUARDED_BY(Locks::breakpoint_lock_);
1968696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
197ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic bool IsBreakpoint(const mirror::ArtMethod* m, uint32_t dex_pc)
19809bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    LOCKS_EXCLUDED(Locks::breakpoint_lock_)
199b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
20009bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
2018696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  for (size_t i = 0; i < gBreakpoints.size(); ++i) {
202a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes    if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == dex_pc) {
2038696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i];
2048696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      return true;
2058696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
2068696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
2078696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return false;
2088696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
2098696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2109e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughesstatic bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) {
2119e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
2129e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // A thread may be suspended for GC; in this code, we really want to know whether
2139e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // there's a debugger suspension active.
2149e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0;
2159e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes}
2169e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
2172dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status)
218b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
22064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
221436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
222436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
223436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
224436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsArrayInstance()) {
225436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_ARRAY;
226436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
227436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
228436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
229436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsArray();
230436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
231436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
2322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Class* DecodeClass(JDWP::RefTypeId id, JDWP::JdwpError& status)
233b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
23564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
236436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
237436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
238436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
239436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsClass()) {
240436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_CLASS;
241436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
242436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
243436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
244436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsClass();
245436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
246436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
247221229cb523f849f165fdafbf9785010963715daElliott Hughesstatic JDWP::JdwpError DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, Thread*& thread)
248a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
249b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
250b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2512dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_peer = gRegistry->Get<mirror::Object*>(thread_id);
25264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_peer == NULL || thread_peer == ObjectRegistry::kInvalidObject) {
253221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't even an object.
254221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_OBJECT;
255221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
256221229cb523f849f165fdafbf9785010963715daElliott Hughes
2572dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
258221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) {
259221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't a thread.
260221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_THREAD;
261221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
262221229cb523f849f165fdafbf9785010963715daElliott Hughes
263221229cb523f849f165fdafbf9785010963715daElliott Hughes  thread = Thread::FromManagedThread(soa, thread_peer);
264221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (thread == NULL) {
265221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This is a java.lang.Thread without a Thread*. Must be a zombie.
266221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_THREAD_NOT_ALIVE;
267436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
268221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
269436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
270436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
27124437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) {
27224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // JDWP deliberately uses the descriptor characters' ASCII values for its enum.
27324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // Note that by "basic" we mean that we don't get more specific than JT_OBJECT.
27424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  return static_cast<JDWP::JdwpTag>(descriptor[0]);
27524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
27624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
2772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic JDWP::JdwpTag TagFromClass(mirror::Class* c)
278b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
27986b0010c79ef95b5333cd540b7d3af34a9f1a643Elliott Hughes  CHECK(c != NULL);
28024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsArrayClass()) {
28124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_ARRAY;
28224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
28324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
2843d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
28524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsStringClass()) {
28624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_STRING;
28724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  } else if (c->IsClassClass()) {
28824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_CLASS_OBJECT;
2893d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else if (class_linker->FindSystemClass("Ljava/lang/Thread;")->IsAssignableFrom(c)) {
29024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_THREAD;
2913d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else if (class_linker->FindSystemClass("Ljava/lang/ThreadGroup;")->IsAssignableFrom(c)) {
29224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_THREAD_GROUP;
2933d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else if (class_linker->FindSystemClass("Ljava/lang/ClassLoader;")->IsAssignableFrom(c)) {
29424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_CLASS_LOADER;
29524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  } else {
29624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_OBJECT;
29724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
29824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
29924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
30024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes/*
30124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Objects declared to hold Object might actually hold a more specific
30224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * type.  The debugger may take a special interest in these (e.g. it
30324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * wants to display the contents of Strings), so we want to return an
30424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * appropriate tag.
30524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes *
30624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Null objects are tagged JT_OBJECT.
30724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes */
3082dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic JDWP::JdwpTag TagFromObject(const mirror::Object* o)
309b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
31024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(o->GetClass());
31124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
31224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
31324437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic bool IsPrimitiveTag(JDWP::JdwpTag tag) {
31424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  switch (tag) {
31524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BOOLEAN:
31624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BYTE:
31724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_CHAR:
31824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_FLOAT:
31924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_DOUBLE:
32024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_INT:
32124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_LONG:
32224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_SHORT:
32324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_VOID:
32424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return true;
32524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  default:
32624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return false;
32724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
32824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
32924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
3303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
3313bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Handle one of the JDWP name/value pairs.
3323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
3333bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * JDWP options are:
3343bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  help: if specified, show help message and bail
3353bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  transport: may be dt_socket or dt_shmem
3363bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  address: for dt_socket, "host:port", or just "port" when listening
3373bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  server: if "y", wait for debugger to attach; if "n", attach to debugger
3383bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  timeout: how long to wait for debugger to connect / listen
3393bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
3403bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Useful with server=n (these aren't supported yet):
3413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onthrow=<exception-name>: connect to debugger when exception thrown
3423bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onuncaught=y|n: connect to debugger when uncaught exception thrown
3433bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  launch=<command-line>: launch the debugger itself
3443bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
3453bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * The "transport" option is required, as is "address" if server=n.
3463bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
3473bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool ParseJdwpOption(const std::string& name, const std::string& value) {
3483bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (name == "transport") {
3493bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "dt_socket") {
350376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
3513bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "dt_android_adb") {
352376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
3533bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
3543bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP transport not supported: " << value;
3553bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
3563bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
3573bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "server") {
3583bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
359376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = false;
3603bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
361376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = true;
3623bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
3633bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
3643bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
3653bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
3663bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "suspend") {
3673bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
368376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = false;
3693bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
370376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = true;
3713bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
3723bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
3733bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
3743bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
3753bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "address") {
3763bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* this is either <port> or <host>:<port> */
3773bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string port_string;
378376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.host.clear();
3793bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type colon = value.find(':');
3803bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (colon != std::string::npos) {
381376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.host = value.substr(0, colon);
3823bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value.substr(colon + 1);
3833bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
3843bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value;
3853bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
3863bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (port_string.empty()) {
3873bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address missing port: " << value;
3883bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
3893bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
3903bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    char* end;
391ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    uint64_t port = strtoul(port_string.c_str(), &end, 10);
392ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    if (*end != '\0' || port > 0xffff) {
3933bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address has junk in port field: " << value;
3943bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
3953bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
396376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.port = port;
3973bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
3983bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* valid but unsupported */
3993bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
4003bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
4013bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
4023bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
4033bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
4043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
4053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
4063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
4073bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
4083bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
4093bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * "transport=dt_socket,address=8000,server=y,suspend=n"
4103bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
4113bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesbool Dbg::ParseJdwpOptions(const std::string& options) {
4124dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "ParseJdwpOptions: " << options;
41347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
4143bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  std::vector<std::string> pairs;
4153bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Split(options, ',', pairs);
4163bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
4173bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  for (size_t i = 0; i < pairs.size(); ++i) {
4183bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type equals = pairs[i].find('=');
4193bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (equals == std::string::npos) {
4203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
4213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4233bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
4243bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
4253bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
426376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
4273bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP transport: " << options;
4283bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
429376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
4303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
4313bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    return false;
4323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
4333bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
4343bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gJdwpConfigured = true;
4353bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
4363bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
4373bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
438d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StartJdwp() {
439c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!gJdwpAllowed || !IsJdwpConfigured()) {
440376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    // No JDWP for you!
441376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    return;
442376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  }
443376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes
444475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  CHECK(gRegistry == NULL);
445475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gRegistry = new ObjectRegistry;
446475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
447d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // Init JDWP if the debugger is enabled. This may connect out to a
448d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger, passively listen for a debugger, or block waiting for a
449d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger.
450376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
451376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState == NULL) {
452f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // We probably failed because some other process has the port already, which means that
453f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // if we don't abort the user is likely to think they're talking to us when they're actually
454f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // talking to that other process.
4553d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Debugger thread failed to initialize";
456d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
457d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes
458d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // If a debugger has already attached, send the "welcome" message.
459d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // This may cause us to suspend all threads.
460376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState->IsActive()) {
46100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
462376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    if (!gJdwpState->PostVMStart()) {
4633d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes      LOG(WARNING) << "Failed to post 'start' message to debugger";
464d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes    }
465d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
466872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
467872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
468d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StopJdwp() {
469376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  delete gJdwpState;
470475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  delete gRegistry;
471475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gRegistry = NULL;
472872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
473872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
474767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesvoid Dbg::GcDidFinish() {
475767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
47600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
47781ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes    LOG(DEBUG) << "Sending heap info to DDM";
4787162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(gDdmHpifWhen);
479767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
480767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
48100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
48281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes    LOG(DEBUG) << "Dumping heap to DDM";
4836a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(false);
484767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
485767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
48600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
487767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(DEBUG) << "Dumping native heap to DDM";
4886a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(true);
489767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
490767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
491767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4924ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesvoid Dbg::SetJdwpAllowed(bool allowed) {
4934ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes  gJdwpAllowed = allowed;
4944ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes}
4954ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
496872d4ec7225444d9400d30f9027247deb91012fdElliott HughesDebugInvokeReq* Dbg::GetInvokeReq() {
497475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return Thread::Current()->GetInvokeReq();
498475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
499475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
500475fc23a4a7f35d1be87ea0b06c80df317a720acElliott HughesThread* Dbg::GetDebugThread() {
501475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
502475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
503475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
504475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::ClearWaitForEventThread() {
505475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gJdwpState->ClearWaitForEventThread();
506872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
507872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
508872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Connected() {
5093bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(!gDebuggerConnected);
5104dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "JDWP has attached";
5113bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gDebuggerConnected = true;
5128696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = false;
5138696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
5148696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
5158696433d1b3d8ba15288483b777edd888de69135Elliott Hughesvoid Dbg::Disposed() {
5168696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = true;
5178696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
5188696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
5198696433d1b3d8ba15288483b777edd888de69135Elliott Hughesbool Dbg::IsDisposed() {
5208696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return gDisposed;
521872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
522872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
523a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::GoActive() {
524a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Enable all debugging features, including scans for breakpoints.
525a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // This is a no-op if we're already active.
526a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Only called from the JDWP handler thread.
527a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (gDebuggerActive) {
528a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return;
529a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
530a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
531c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  {
532c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected?
53309bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
534c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    CHECK_EQ(gBreakpoints.size(), 0U);
535c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  }
536a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
53762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
53862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
53962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
54062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
54162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_NE(old_state, kRunnable);
54262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetInstrumentation()->AddListener(&gDebugInstrumentationListener,
54362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                             instrumentation::Instrumentation::kMethodEntered |
54462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                             instrumentation::Instrumentation::kMethodExited |
54514dd5a82ad85a28612bf86e755ff929d767cea4cJeff Hao                                             instrumentation::Instrumentation::kDexPcMoved |
54614dd5a82ad85a28612bf86e755ff929d767cea4cJeff Hao                                             instrumentation::Instrumentation::kExceptionCaught);
547a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  gDebuggerActive = true;
54862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
54962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
55062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
55162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  LOG(INFO) << "Debugger is active";
552872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
553872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
554872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Disconnected() {
555234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  CHECK(gDebuggerConnected);
556234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
557c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  LOG(INFO) << "Debugger is no longer active";
558234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
55962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread
56062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener
56162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // and clear the object registry.
56262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
56362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
56462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
56562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
56662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener,
56762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                                instrumentation::Instrumentation::kMethodEntered |
56862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                                instrumentation::Instrumentation::kMethodExited |
56914dd5a82ad85a28612bf86e755ff929d767cea4cJeff Hao                                                instrumentation::Instrumentation::kDexPcMoved |
57014dd5a82ad85a28612bf86e755ff929d767cea4cJeff Hao                                                instrumentation::Instrumentation::kExceptionCaught);
571c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  gDebuggerActive = false;
572234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gRegistry->Clear();
573234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gDebuggerConnected = false;
57462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
57562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
576872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
577872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
578c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsDebuggerActive() {
5793bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gDebuggerActive;
580872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
581872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
582c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsJdwpConfigured() {
5833bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gJdwpConfigured;
584872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
585872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
586872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesint64_t Dbg::LastDebuggerActivity() {
587ca9515205010099d006ac2fac244348a1e673dcbElliott Hughes  return gJdwpState->LastDebuggerActivity();
588872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
589872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
590872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::UndoDebuggerSuspensions() {
591234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
592872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
593872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
59488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetClassName(JDWP::RefTypeId class_id) {
5952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id);
5963d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (o == NULL) {
597436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return "NULL";
5987b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
59964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
60088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id));
6013d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
6023d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (!o->IsClass()) {
6037934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    return StringPrintf("non-class %p", o);  // This is only used for debugging output anyway.
6043d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
605436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return DescriptorToName(ClassHelper(o->AsClass()).GetDescriptor());
6063d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes}
6073d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
60888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) {
609436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
6102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
611436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
612436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
6132435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
61488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  class_object_id = gRegistry->Add(c);
615436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
6168696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
6178696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
61888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) {
6193d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
6202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
6213d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c == NULL) {
6223d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
6233d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
6243d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c->IsInterface()) {
6253d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    // http://code.google.com/p/android/issues/detail?id=20856
62688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = 0;
6273d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else {
62888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = gRegistry->Add(c->GetSuperClass());
6293d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
6303d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
631872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
632872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
633436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
6342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
63564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
636436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
637436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
638436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader()));
639436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
640872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
641872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
642436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
643436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
6442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
645436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
646436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
6477b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
648436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
649436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask;
650436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
651436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  // Set ACC_SUPER; dex files don't contain this flag, but all classes are supposed to have it set.
652436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
653436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  access_flags |= kAccSuper;
654436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
655436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAdd4BE(pReply, access_flags);
656436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
657436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
658872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
659872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
660f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott HughesJDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply)
661f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
6622dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
66364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
664f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
665f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
666f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
667f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  // Ensure all threads are suspended while we read objects' lock words.
668f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Thread* self = Thread::Current();
669f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Locks::mutator_lock_->SharedUnlock(self);
670f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Locks::mutator_lock_->ExclusiveLock(self);
671f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
672f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  MonitorInfo monitor_info(o);
673f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
674f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Locks::mutator_lock_->ExclusiveUnlock(self);
675f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Locks::mutator_lock_->SharedLock(self);
676f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
677f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  if (monitor_info.owner != NULL) {
678f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner->GetPeer()));
679f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else {
680f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    expandBufAddObjectId(reply, gRegistry->Add(NULL));
681f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
682f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  expandBufAdd4BE(reply, monitor_info.entry_count);
683f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  expandBufAdd4BE(reply, monitor_info.waiters.size());
684f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  for (size_t i = 0; i < monitor_info.waiters.size(); ++i) {
685f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters[i]->GetPeer()));
686f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
687f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  return JDWP::ERR_NONE;
688f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes}
689f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
690734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott HughesJDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id,
691734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes                                      std::vector<JDWP::ObjectId>& monitors,
692734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes                                      std::vector<uint32_t>& stack_depths)
6934993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
6944993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
6954993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
6964993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  Thread* thread;
6974993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
6984993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  if (error != JDWP::ERR_NONE) {
6994993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return error;
7004993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
7014993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
7024993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
7034993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
7044993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
7054993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  struct OwnedMonitorVisitor : public StackVisitor {
7067a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    OwnedMonitorVisitor(Thread* thread, Context* context)
7074993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
7087a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context), current_stack_depth(0) {}
7094993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
7104993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
7114993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // annotalysis.
7124993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
7134993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      if (!GetMethod()->IsRuntimeMethod()) {
7144993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        Monitor::VisitLocks(this, AppendOwnedMonitors, this);
715734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes        ++current_stack_depth;
7164993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      }
7174993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      return true;
7184993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
7194993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
7202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) {
7217a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg);
722734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes      visitor->monitors.push_back(owned_monitor);
723734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes      visitor->stack_depths.push_back(visitor->current_stack_depth);
7244993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
7254993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
726734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    size_t current_stack_depth;
7272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    std::vector<mirror::Object*> monitors;
728734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    std::vector<uint32_t> stack_depths;
7294993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  };
7307a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  UniquePtr<Context> context(Context::Create());
7317a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  OwnedMonitorVisitor visitor(thread, context.get());
7324993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  visitor.WalkStack();
7334993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
7344993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  for (size_t i = 0; i < visitor.monitors.size(); ++i) {
7354993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    monitors.push_back(gRegistry->Add(visitor.monitors[i]));
736734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    stack_depths.push_back(visitor.stack_depths[i]);
7374993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
7384993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
7394993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  return JDWP::ERR_NONE;
7404993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes}
7414993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
742f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott HughesJDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, JDWP::ObjectId& contended_monitor)
743f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
744f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
745f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
746f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
747f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
748f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
749f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
750f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
751f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
752f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
753f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
754f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
755f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  contended_monitor = gRegistry->Add(Monitor::GetContendedMonitor(thread));
756f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
757f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
758f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
759f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
760ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott HughesJDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids,
761ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes                                       std::vector<uint64_t>& counts)
762ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
7632dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
764ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  counts.clear();
765ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  for (size_t i = 0; i < class_ids.size(); ++i) {
766ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    JDWP::JdwpError status;
7672dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_ids[i], status);
768ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    if (c == NULL) {
769ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes      return status;
770ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    }
771ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    classes.push_back(c);
772ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    counts.push_back(0);
773ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  }
774ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes
775ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  Runtime::Current()->GetHeap()->CountInstances(classes, false, &counts[0]);
776ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  return JDWP::ERR_NONE;
777ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes}
778ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes
7793b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott HughesJDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, std::vector<JDWP::ObjectId>& instances)
7803b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
7813b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  JDWP::JdwpError status;
7822dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
7833b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  if (c == NULL) {
7843b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    return status;
7853b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
7863b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes
7872dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
7883b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances);
7893b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
7903b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    instances.push_back(gRegistry->Add(raw_instances[i]));
7913b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
7923b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  return JDWP::ERR_NONE;
7933b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes}
7943b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes
7950cbaff584244ee767027aff35cd3c625aaee2994Elliott HughesJDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count,
7960cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes                                         std::vector<JDWP::ObjectId>& referring_objects)
7970cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
7982dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
79964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
8000cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
8010cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
8020cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes
8032dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
8040cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  Runtime::Current()->GetHeap()->GetReferringObjects(o, max_count, raw_instances);
8050cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
8060cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    referring_objects.push_back(gRegistry->Add(raw_instances[i]));
8070cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
8080cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  return JDWP::ERR_NONE;
8090cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes}
8100cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes
81164f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id)
81264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
81364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisableCollection(object_id);
81464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
81564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
81664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
81764f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id)
81864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
81964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->EnableCollection(object_id);
82064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
82164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
82264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
82364f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected)
82464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
82564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  is_collected = gRegistry->IsCollected(object_id);
82664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
82764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
82864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
82964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count)
83064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
83164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisposeObject(object_id, reference_count);
83264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
83364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
83488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
835436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
8362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
837436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
838436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
8397b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
840436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
841436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAdd1(pReply, c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS);
84288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  expandBufAddRefTypeId(pReply, class_id);
843436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
844872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
845872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
8467b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughesvoid Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) {
847a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Get the complete list of reference classes (i.e. all classes except
848a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // the primitive types).
849a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Returns a newly-allocated buffer full of RefTypeId values.
850a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  struct ClassListCreator {
851ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) {
8527b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    }
8537b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
8542dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static bool Visit(mirror::Class* c, void* arg) {
855a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return reinterpret_cast<ClassListCreator*>(arg)->Visit(c);
856a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
857a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
85864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
85964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
86064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS {
861a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (!c->IsPrimitive()) {
86264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes        classes.push_back(gRegistry->AddRefType(c));
863a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
864a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return true;
865a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
866a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
8677b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    std::vector<JDWP::RefTypeId>& classes;
868a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
869a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
8707b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  ClassListCreator clc(classes);
871a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc);
872872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
873872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
87488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) {
875436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
8762dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
877436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
878436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
8797b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
8807b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
881a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (c->IsArrayClass()) {
882a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
883a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = JDWP::TT_ARRAY;
884a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
885a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsErroneous()) {
886a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_ERROR;
887a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    } else {
888a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED;
889a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
890a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
891a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
892a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
893a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (pDescriptor != NULL) {
8946d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    *pDescriptor = ClassHelper(c).GetDescriptor();
895a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
896436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
897872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
898872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
899c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughesvoid Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) {
9002dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
9016fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes);
9026fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  ids.clear();
9036fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  for (size_t i = 0; i < classes.size(); ++i) {
9046fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes    ids.push_back(gRegistry->Add(classes[i]));
9056fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  }
906872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
907872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
90864f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply)
90964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
9102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
91164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
9122435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
9132435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
9142435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
9152435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  JDWP::JdwpTypeTag type_tag;
916499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  if (o->GetClass()->IsArrayClass()) {
9172435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    type_tag = JDWP::TT_ARRAY;
918499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  } else if (o->GetClass()->IsInterface()) {
9192435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    type_tag = JDWP::TT_INTERFACE;
920499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  } else {
9212435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    type_tag = JDWP::TT_CLASS;
922499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  }
92364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass());
9242435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
9252435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAdd1(pReply, type_tag);
9262435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAddRefTypeId(pReply, type_id);
9272435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
9282435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
929872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
930872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
93188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string& signature) {
9321fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  JDWP::JdwpError status;
9332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
9341fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  if (c == NULL) {
9351fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes    return status;
9367b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
9371fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  signature = ClassHelper(c).GetDescriptor();
9381fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  return JDWP::ERR_NONE;
939872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
940872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
94188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result) {
942436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
9432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
944436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
945436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
9467b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
947436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  result = ClassHelper(c).GetSourceFile();
948436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
949872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
950872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
95188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) {
9522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
95364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
954546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
955546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
956546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  tag = TagFromObject(o);
957546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  return JDWP::ERR_NONE;
958872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
959872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
960aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughessize_t Dbg::GetTagWidth(JDWP::JdwpTag tag) {
961dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  switch (tag) {
962dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_VOID:
963dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 0;
964dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BYTE:
965dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BOOLEAN:
966dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 1;
967dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CHAR:
968dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_SHORT:
969dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 2;
970dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_FLOAT:
971dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_INT:
972dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 4;
973dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_ARRAY:
974dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_OBJECT:
975dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_STRING:
976dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD:
977dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD_GROUP:
978dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_LOADER:
979dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_OBJECT:
980dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return sizeof(JDWP::ObjectId);
981dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_DOUBLE:
982dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_LONG:
983dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 8;
984dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  default:
9853d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Unknown tag " << tag;
986dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return -1;
987dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
988872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
989872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
99088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int& length) {
9913d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
9922dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
9933d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (a == NULL) {
9943d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
99524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
9963d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  length = a->GetLength();
9973d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
998872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
999872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
100088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) {
10013d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
10022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
10033d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (a == NULL) {
10043d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
10053d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
100624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
100724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) {
100824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
10093d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
101024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
10116d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor());
101224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
101324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
10143d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd1(pReply, tag);
10153d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd4BE(pReply, count);
10163d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
101724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (IsPrimitiveTag(tag)) {
101824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    size_t width = GetTagWidth(tag);
101924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    uint8_t* dst = expandBufAddSpace(pReply, count * width);
102024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    if (width == 8) {
1021a15e67d5ee5aa9615596cee2be42c2b2caf128c6Ian Rogers      const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t)));
102224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]);
102324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 4) {
1024a15e67d5ee5aa9615596cee2be42c2b2caf128c6Ian Rogers      const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t)));
102524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]);
102624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 2) {
1027a15e67d5ee5aa9615596cee2be42c2b2caf128c6Ian Rogers      const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t)));
102824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]);
102924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else {
1030a15e67d5ee5aa9615596cee2be42c2b2caf128c6Ian Rogers      const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t)));
103124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      memcpy(dst, &src[offset * width], count * width);
103224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
103324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  } else {
10342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>();
103524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    for (int i = 0; i < count; ++i) {
10362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* element = oa->Get(offset + i);
103724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      JDWP::JdwpTag specific_tag = (element != NULL) ? TagFromObject(element) : tag;
103824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAdd1(pReply, specific_tag);
103924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAddObjectId(pReply, gRegistry->Add(element));
104024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
104124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
104224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
10433d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1044872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1045872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
10464b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughestemplate <typename T> void CopyArrayData(mirror::Array* a, JDWP::Request& src, int offset, int count) {
10474b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  DCHECK(a->GetClass()->IsPrimitiveArray());
10484b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
10494b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  T* dst = &(reinterpret_cast<T*>(a->GetRawData(sizeof(T)))[offset * sizeof(T)]);
10504b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  for (int i = 0; i < count; ++i) {
10514b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    *dst++ = src.ReadValue(sizeof(T));
10524b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  }
10534b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
10544b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
105588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count,
10564b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                      JDWP::Request& request)
1057b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
10583d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
10594b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  mirror::Array* dst = DecodeArray(array_id, status);
10604b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (dst == NULL) {
10613d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
10623d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
1063f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
10644b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) {
1065f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
10663d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
1067f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
10684b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  std::string descriptor(ClassHelper(dst->GetClass()).GetDescriptor());
1069f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
1070f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
1071f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  if (IsPrimitiveTag(tag)) {
1072f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    size_t width = GetTagWidth(tag);
1073f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    if (width == 8) {
10744b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint64_t>(dst, request, offset, count);
1075f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 4) {
10764b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint32_t>(dst, request, offset, count);
1077f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 2) {
10784b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint16_t>(dst, request, offset, count);
1079f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else {
10804b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint8_t>(dst, request, offset, count);
1081f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1082f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  } else {
10834b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>();
1084f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    for (int i = 0; i < count; ++i) {
10854b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      JDWP::ObjectId id = request.ReadObjectId();
10862dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
108764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes      if (o == ObjectRegistry::kInvalidObject) {
1088436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
1089436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes      }
1090436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes      oa->Set(offset + i, o);
1091f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1092f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1093f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
10943d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1095872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1096872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
10977b3cdfcca472b779cf8745fb8460935e56229f11Elliott HughesJDWP::ObjectId Dbg::CreateString(const std::string& str) {
10982dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  return gRegistry->Add(mirror::String::AllocFromModifiedUtf8(Thread::Current(), str.c_str()));
1099872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1100872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
110188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) {
1102436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11032dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1104436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1105436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
11067b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
110750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  new_object = gRegistry->Add(c->AllocObject(Thread::Current()));
1108436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1109872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1110872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1111bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes/*
1112bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".
1113bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes */
111488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length,
111500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                       JDWP::ObjectId& new_array) {
1116436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11172dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(array_class_id, status);
1118436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1119436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
11207b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
11212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  new_array = gRegistry->Add(mirror::Array::Alloc(Thread::Current(), c, length));
1122436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1123872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1124872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
112588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesbool Dbg::MatchType(JDWP::RefTypeId instance_class_id, JDWP::RefTypeId class_id) {
1126436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c1 = DecodeClass(instance_class_id, status);
1128a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  CHECK(c1 != NULL);
11292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c2 = DecodeClass(class_id, status);
1130a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  CHECK(c2 != NULL);
1131a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  return c1->IsAssignableFrom(c2);
1132872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1133872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1134ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::FieldId ToFieldId(const mirror::ArtField* f)
1135b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
113603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes#ifdef MOVING_GARBAGE_COLLECTOR
1137872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes  UNIMPLEMENTED(FATAL);
113803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes#else
113903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f));
114003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes#endif
114103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
114203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1143ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::MethodId ToMethodId(const mirror::ArtMethod* m)
1144b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
114503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes#ifdef MOVING_GARBAGE_COLLECTOR
114603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  UNIMPLEMENTED(FATAL);
114703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes#else
114803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m));
114903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes#endif
115003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
115103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1152ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtField* FromFieldId(JDWP::FieldId fid)
1153b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1154aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes#ifdef MOVING_GARBAGE_COLLECTOR
1155aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  UNIMPLEMENTED(FATAL);
1156aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes#else
1157ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtField*>(static_cast<uintptr_t>(fid));
1158aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes#endif
1159aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes}
1160aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1161ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtMethod* FromMethodId(JDWP::MethodId mid)
1162b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
116303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes#ifdef MOVING_GARBAGE_COLLECTOR
116403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  UNIMPLEMENTED(FATAL);
116503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes#else
1166ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtMethod*>(static_cast<uintptr_t>(mid));
116703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes#endif
116803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
116903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1170ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic void SetLocation(JDWP::JdwpLocation& location, mirror::ArtMethod* m, uint32_t dex_pc)
1171b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
117291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (m == NULL) {
117391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    memset(&location, 0, sizeof(location));
117491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  } else {
11752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = m->GetDeclaringClass();
1176748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    location.type_tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
1177748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    location.class_id = gRegistry->Add(c);
1178748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    location.method_id = ToMethodId(m);
11790399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    location.dex_pc = dex_pc;
118091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
1181d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes}
1182d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
1183a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetMethodName(JDWP::MethodId method_id)
1184b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1185ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
11866d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  return MethodHelper(m).GetName();
1187872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1188872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1189a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetFieldName(JDWP::FieldId field_id)
1190a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1191ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
1192a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes  return FieldHelper(f).GetName();
1193a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes}
1194a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes
1195a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes/*
1196a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * Augment the access flags for synthetic methods and fields by setting
1197a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * the (as described by the spec) "0xf0000000 bit".  Also, strip out any
1198a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * flags not specified by the Java programming language.
1199a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes */
1200a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughesstatic uint32_t MangleAccessFlags(uint32_t accessFlags) {
1201a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  accessFlags &= kAccJavaFlagsMask;
1202a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  if ((accessFlags & kAccSynthetic) != 0) {
1203a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    accessFlags |= 0xf0000000;
1204a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1205a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return accessFlags;
1206872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1207872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1208dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughesstatic const uint16_t kEclipseWorkaroundSlot = 1000;
1209dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1210dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes/*
1211dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes * Eclipse appears to expect that the "this" reference is in slot zero.
1212dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes * If it's not, the "variables" display will show two copies of "this",
1213dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes * possibly because it gets "this" from SF.ThisObject and then displays
1214dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes * all locals with nonzero slot numbers.
1215dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes *
1216dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes * So, we remap the item in slot 0 to 1000, and remap "this" to zero.  On
1217dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes * SF.GetValues / SF.SetValues we map them back.
1218c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes *
1219c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes * TODO: jdb uses the value to determine whether a variable is a local or an argument,
1220c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes * by checking whether it's less than the number of arguments. To make that work, we'd
1221c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes * have to "mangle" all the arguments to come first, not just the implicit argument 'this'.
1222dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes */
1223dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughesstatic uint16_t MangleSlot(uint16_t slot, const char* name) {
1224dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  uint16_t newSlot = slot;
1225dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  if (strcmp(name, "this") == 0) {
1226dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    newSlot = 0;
1227dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  } else if (slot == 0) {
1228dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    newSlot = kEclipseWorkaroundSlot;
1229dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1230dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  return newSlot;
1231dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1232dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1233ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic uint16_t DemangleSlot(uint16_t slot, mirror::ArtMethod* m)
1234b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1235dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  if (slot == kEclipseWorkaroundSlot) {
123668fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes    return 0;
1237dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  } else if (slot == 0) {
123891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
1239caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    CHECK(code_item != NULL) << PrettyMethod(m);
12406d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    return code_item->registers_size_ - code_item->ins_size_;
1241dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
124268fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  return slot;
1243dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1244dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
124588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1246436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
12472dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1248436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1249436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
12507b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1251a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1252a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t instance_field_count = c->NumInstanceFields();
1253a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t static_field_count = c->NumStaticFields();
1254a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1255a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, instance_field_count + static_field_count);
1256a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1257a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < instance_field_count + static_field_count; ++i) {
1258ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count);
12596d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    FieldHelper fh(f);
1260a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddFieldId(pReply, ToFieldId(f));
12616d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, fh.GetName());
12626d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, fh.GetTypeDescriptor());
1263c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1264a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1265a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1266a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1267a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags()));
1268a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1269436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1270a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1271a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
127288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic,
127300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                           JDWP::ExpandBuf* pReply) {
1274436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
12752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1276436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1277436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
12787b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1279a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1280a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t direct_method_count = c->NumDirectMethods();
1281a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t virtual_method_count = c->NumVirtualMethods();
1282a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1283a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, direct_method_count + virtual_method_count);
1284a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1285a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) {
1286ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
12876d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    MethodHelper mh(m);
1288a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddMethodId(pReply, ToMethodId(m));
12896d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, mh.GetName());
12904740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes    expandBufAddUtf8String(pReply, mh.GetSignature());
1291c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1292a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1293a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1294a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1295a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags()));
1296a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1297436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1298a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1299a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
130088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1301436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
13022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1303436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1304436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
13057b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1306436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
1307436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  ClassHelper kh(c);
1308d24e264ff85ad8c6f142ac6d33055fdc1881fa2fIan Rogers  size_t interface_count = kh.NumDirectInterfaces();
1309a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, interface_count);
1310a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < interface_count; ++i) {
131164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    expandBufAddRefTypeId(pReply, gRegistry->AddRefType(kh.GetDirectInterface(i)));
1312a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1313436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1314872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1315872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
131688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply)
1317b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
131803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  struct DebugCallbackContext {
131903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    int numItems;
132003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    JDWP::ExpandBuf* pReply;
132103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
13222435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    static bool Callback(void* context, uint32_t address, uint32_t line_number) {
132303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
132403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      expandBufAdd8BE(pContext->pReply, address);
13252435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      expandBufAdd4BE(pContext->pReply, line_number);
132603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      pContext->numItems++;
1327f2910eef247b45ce1d489e323b36b5de6b6157aaSebastien Hertz      return false;
132803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
132903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
1330ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
13316d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  MethodHelper mh(m);
133203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  uint64_t start, end;
133303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  if (m->IsNative()) {
133403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = -1;
133503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    end = -1;
133603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  } else {
133703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = 0;
133814f0db92225d34622fa5cb1a6dc9287334aaf6c7jeffhao    // Return the index of the last instruction
133914f0db92225d34622fa5cb1a6dc9287334aaf6c7jeffhao    end = mh.GetCodeItem()->insns_size_in_code_units_ - 1;
134003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  }
134103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
134203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, start);
134303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, end);
134403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
134503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  // Add numLines later
134603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  size_t numLinesOffset = expandBufGetLength(pReply);
134703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd4BE(pReply, 0);
134803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
134903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  DebugCallbackContext context;
135003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.numItems = 0;
135103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.pReply = pReply;
135203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
13536d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(),
13546d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers                                  DebugCallbackContext::Callback, NULL, &context);
135503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
135603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems);
1357872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1358872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
135988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1360dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  struct DebugCallbackContext {
1361dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    JDWP::ExpandBuf* pReply;
1362c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    size_t variable_count;
1363c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    bool with_generic;
1364dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1365c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, const char* name, const char* descriptor, const char* signature) {
1366dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1367dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1368ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes      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, name));
1369dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
137068fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes      slot = MangleSlot(slot, name);
137168fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
1372dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd8BE(pContext->pReply, startAddress);
1373dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, name);
1374dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, descriptor);
1375c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      if (pContext->with_generic) {
1376dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes        expandBufAddUtf8String(pContext->pReply, signature);
1377dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      }
1378dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, endAddress - startAddress);
1379dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, slot);
1380dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1381c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      ++pContext->variable_count;
1382dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    }
1383dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  };
1384ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
13856d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  MethodHelper mh(m);
13866d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  const DexFile::CodeItem* code_item = mh.GetCodeItem();
1387dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1388c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // arg_count considers doubles and longs to take 2 units.
1389c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // variable_count considers everything to take 1 unit.
13906d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  std::string shorty(mh.GetShorty());
1391ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  expandBufAdd4BE(pReply, mirror::ArtMethod::NumArgRegisters(shorty));
1392dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1393c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // We don't know the total number of variables yet, so leave a blank and update it later.
1394c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  size_t variable_count_offset = expandBufGetLength(pReply);
1395dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  expandBufAdd4BE(pReply, 0);
1396dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1397dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  DebugCallbackContext context;
1398dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  context.pReply = pReply;
1399c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.variable_count = 0;
1400c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.with_generic = with_generic;
1401dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
14026d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL,
14036d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers                                  DebugCallbackContext::Callback, &context);
1404dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1405c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count);
1406872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1407872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
14089777ba230c83a0edcbda2cf7b208339e77bf171bElliott HughesJDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id,
14099777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes                                  std::vector<uint8_t>& bytecodes)
14109777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1411ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
14129777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  if (m == NULL) {
14139777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    return JDWP::ERR_INVALID_METHODID;
14149777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
14159777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  MethodHelper mh(m);
14169777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const DexFile::CodeItem* code_item = mh.GetCodeItem();
14179777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  size_t byte_count = code_item->insns_size_in_code_units_ * 2;
14189777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_);
14199777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* end = begin + byte_count;
14209777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  for (const uint8_t* p = begin; p != end; ++p) {
14219777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    bytecodes.push_back(*p);
14229777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
14239777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  return JDWP::ERR_NONE;
14249777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes}
14259777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes
142688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) {
142788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
1428872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1429872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
143088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) {
143188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
1432872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1433872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
143488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id,
143588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                         JDWP::FieldId field_id, JDWP::ExpandBuf* pReply,
143600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         bool is_static)
1437b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
14380cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  JDWP::JdwpError status;
14392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(ref_type_id, status);
144088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  if (ref_type_id != 0 && c == NULL) {
14410cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    return status;
14420cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
14430cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
14442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
144564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
14463f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
14473f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1448ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
14490cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
14502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* receiver_class = c;
14510cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class == NULL && o != NULL) {
14520cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    receiver_class = o->GetClass();
14530cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
14540cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we give up now if receiver_class is NULL?
14550cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) {
14560cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class);
14573f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_FIELDID;
14583f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1459aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
14600cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
14610cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
14620cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
14630cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
14640cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
14650cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
14660cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
14670cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
14680cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
14690cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
14700cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
14710dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
14720dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
14730dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
14740cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
14756d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
1476aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1477aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  if (IsPrimitiveTag(tag)) {
1478aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    expandBufAdd1(pReply, tag);
1479aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) {
1480aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes      expandBufAdd1(pReply, f->Get32(o));
1481aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) {
1482aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes      expandBufAdd2BE(pReply, f->Get32(o));
1483aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) {
1484aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes      expandBufAdd4BE(pReply, f->Get32(o));
1485aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1486aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes      expandBufAdd8BE(pReply, f->Get64(o));
1487aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    } else {
14883d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes      LOG(FATAL) << "Unknown tag: " << tag;
1489aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    }
1490aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
14912dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* value = f->GetObject(o);
1492aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    expandBufAdd1(pReply, TagFromObject(value));
1493aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    expandBufAddObjectId(pReply, gRegistry->Add(value));
1494aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
14953f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  return JDWP::ERR_NONE;
1496872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1497872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
149888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id,
149900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ExpandBuf* pReply) {
150088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(0, object_id, field_id, pReply, false);
15013f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
15023f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
150388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, JDWP::ExpandBuf* pReply) {
150488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true);
15053f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
15063f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
150788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id,
150800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         uint64_t value, int width, bool is_static)
1509b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
15102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
151164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
15123f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
15133f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1514ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
15150cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
15160cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
15170cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
15180cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
15190cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
15200cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
15210cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
15220cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
15230cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
15240cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
15250cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
15263f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
15270dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
15280dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
15290dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
1530aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
15316d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
1532aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1533aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  if (IsPrimitiveTag(tag)) {
1534aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
15351bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_EQ(width, 8);
1536aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes      f->Set64(o, value);
1537aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    } else {
15381bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_LE(width, 4);
1539aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes      f->Set32(o, value);
1540aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    }
1541aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
15422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* v = gRegistry->Get<mirror::Object*>(value);
154364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (v == ObjectRegistry::kInvalidObject) {
15443d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes      return JDWP::ERR_INVALID_OBJECT;
15453d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    }
15463f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    if (v != NULL) {
15472dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* field_type = FieldHelper(f).GetType();
15483f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      if (!field_type->IsAssignableFrom(v->GetClass())) {
15493f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
15503f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      }
15513f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
15523d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    f->SetObject(o, v);
1553aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
15543d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
15553d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1556872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1557872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
155888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value,
155900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   int width) {
156088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(object_id, field_id, value, width, false);
1561872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1562872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
156388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) {
156488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(0, field_id, value, width, true);
1565872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1566872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
156788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::StringToUtf8(JDWP::ObjectId string_id) {
15682dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = gRegistry->Get<mirror::String*>(string_id);
156968fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  return s->ToModifiedUtf8();
1570872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1571872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1572221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string& name) {
1573a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  ScopedObjectAccessUnchecked soa(Thread::Current());
1574a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1575221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1576221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1577221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) {
1578221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1579a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1580221229cb523f849f165fdafbf9785010963715daElliott Hughes
1581221229cb523f849f165fdafbf9785010963715daElliott Hughes  // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName.
15822dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
1583ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* java_lang_Thread_name_field =
15842dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      soa.DecodeField(WellKnownClasses::java_lang_Thread_name);
15852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s =
15862dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object));
1587221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (s != NULL) {
1588221229cb523f849f165fdafbf9785010963715daElliott Hughes    name = s->ToModifiedUtf8();
1589221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1590221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1591872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1592872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1593221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
159400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
15952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
159664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_object == ObjectRegistry::kInvalidObject) {
15972435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
15982435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
15992435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
16002435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Okay, so it's an object, but is it actually a thread?
160150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1602221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1603221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1604221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1605221229cb523f849f165fdafbf9785010963715daElliott Hughes    // Zombie threads are in the null group.
1606221229cb523f849f165fdafbf9785010963715daElliott Hughes    expandBufAddObjectId(pReply, JDWP::ObjectId(0));
1607221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_NONE;
1608221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1609221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1610221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
16112435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
1612499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
16132dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Thread;");
1614499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(c != NULL);
1615ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;");
1616499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(f != NULL);
16172dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(thread_object);
1618499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(group != NULL);
16192435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  JDWP::ObjectId thread_group_id = gRegistry->Add(group);
16202435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
16212435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAddObjectId(pReply, thread_group_id);
16222435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1623872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1624872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
162588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id) {
162600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
16272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
1628499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(thread_group != NULL);
1629499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
16302dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;");
1631499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(c != NULL);
1632ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("name", "Ljava/lang/String;");
1633499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(f != NULL);
16342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group));
1635499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  return s->ToModifiedUtf8();
1636872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1637872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
163888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id) {
16392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
16404e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  CHECK(thread_group != NULL);
16414e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes
16422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;");
16434e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  CHECK(c != NULL);
1644ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;");
16454e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  CHECK(f != NULL);
16462dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* parent = f->GetObject(thread_group);
16474e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  return gRegistry->Add(parent);
1648872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1649872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1650872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetSystemThreadGroupId() {
165100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
1652ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup);
16532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
1654365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
1655872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1656872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1657872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetMainThreadGroupId() {
165800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1659ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup);
16602dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
1661365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
1662872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1663872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1664920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff HaoJDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) {
1665920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  switch (state) {
1666920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kBlocked:
1667920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_MONITOR;
1668920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kNative:
1669920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kRunnable:
1670920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSuspended:
1671920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_RUNNING;
1672920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSleeping:
1673920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_SLEEPING;
1674920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kStarting:
1675920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTerminated:
1676920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_ZOMBIE;
1677920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTimedWaiting:
1678920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSend:
1679920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSuspension:
1680920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerToAttach:
1681920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForGcToComplete:
1682920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForCheckPointsToRun:
1683920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForJniOnLoad:
1684920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForSignalCatcherOutput:
1685920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainDebuggerLoop:
1686920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainSignalCatcherLoop:
1687920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingPerformingGc:
1688920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaiting:
1689920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_WAIT;
1690920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      // Don't add a 'default' here so the compiler can spot incompatible enum changes.
1691920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  }
1692920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  LOG(FATAL) << "Unknown thread state: " << state;
1693920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  return JDWP::TS_ZOMBIE;
1694920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao}
1695920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao
1696221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, JDWP::JdwpSuspendStatus* pSuspendStatus) {
169700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1698499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
16999e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED;
17009e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
170150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1702221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1703221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1704221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1705221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1706221229cb523f849f165fdafbf9785010963715daElliott Hughes      *pThreadStatus = JDWP::TS_ZOMBIE;
1707221229cb523f849f165fdafbf9785010963715daElliott Hughes      return JDWP::ERR_NONE;
1708221229cb523f849f165fdafbf9785010963715daElliott Hughes    }
1709221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1710499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  }
1711499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
17129e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  if (IsSuspendedForDebugger(soa, thread)) {
17139e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED;
17149e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  }
171500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
1716920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  *pThreadStatus = ToJdwpThreadStatus(thread->GetState());
1717221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1718872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1719872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1720221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
172100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
172250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1723221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1724221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1725221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1726221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
17272435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
172850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
172900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  expandBufAdd4BE(pReply, thread->GetDebugSuspendCount());
17302435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1731872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1732872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1733f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott HughesJDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) {
1734f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccess soa(Thread::Current());
1735f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1736f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
1737f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1738f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
1739f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
1740f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
1741f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  thread->Interrupt();
1742f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
1743f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
1744f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
1745caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughesvoid Dbg::GetThreads(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& thread_ids) {
1746365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  class ThreadListVisitor {
1747365c10235438607541fa2259a5fec48061b90bd8Ian Rogers   public:
17482dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    ThreadListVisitor(const ScopedObjectAccessUnchecked& soa, mirror::Object* desired_thread_group,
174900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                      std::vector<JDWP::ObjectId>& thread_ids)
1750b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
17510dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        : soa_(soa), desired_thread_group_(desired_thread_group), thread_ids_(thread_ids) {}
1752365c10235438607541fa2259a5fec48061b90bd8Ian Rogers
1753a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    static void Visit(Thread* t, void* arg) {
1754a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t);
1755a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1756a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
175700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
175800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
175900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void Visit(Thread* t) NO_THREAD_SAFETY_ANALYSIS {
1760a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (t == Dbg::GetDebugThread()) {
1761a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and
1762a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        // query all threads, so it's easier if we just don't tell them about this thread.
1763a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
1764a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
17652dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* peer = t->GetPeer();
17660dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      if (IsInDesiredThreadGroup(peer)) {
1767120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers        thread_ids_.push_back(gRegistry->Add(peer));
1768a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
1769a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1770a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
1771365c10235438607541fa2259a5fec48061b90bd8Ian Rogers   private:
17722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    bool IsInDesiredThreadGroup(mirror::Object* peer)
17730dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
17740dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      // peer might be NULL if the thread is still starting up.
17750dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      if (peer == NULL) {
17760dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // We can't tell the debugger about this thread yet.
17770dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // TODO: if we identified threads to the debugger by their Thread*
17782dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        // rather than their peer's mirror::Object*, we could fix this.
17790dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // Doing so might help us report ZOMBIE threads too.
17800dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        return false;
17810dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      }
1782c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      // Do we want threads from all thread groups?
1783c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      if (desired_thread_group_ == NULL) {
1784c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao        return true;
1785c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      }
17862dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* group = soa_.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(peer);
17870dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      return (group == desired_thread_group_);
17880dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    }
17890dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao
1790dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier    const ScopedObjectAccessUnchecked& soa_;
17912dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* const desired_thread_group_;
1792caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    std::vector<JDWP::ObjectId>& thread_ids_;
1793a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
1794a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
179500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
17962dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
179700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ThreadListVisitor tlv(soa, thread_group, thread_ids);
179850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1799f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughes  Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv);
1800a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes}
1801a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
1802caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughesvoid Dbg::GetChildThreadGroups(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& child_thread_group_ids) {
180300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
18042dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
1805caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes
1806caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Get the ArrayList<ThreadGroup> "groups" out of this thread group...
1807ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* groups_field = thread_group->GetClass()->FindInstanceField("groups", "Ljava/util/List;");
18082dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* groups_array_list = groups_field->GetObject(thread_group);
1809872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1810caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Get the array and size out of the ArrayList<ThreadGroup>...
1811ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* array_field = groups_array_list->GetClass()->FindInstanceField("array", "[Ljava/lang/Object;");
1812ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* size_field = groups_array_list->GetClass()->FindInstanceField("size", "I");
18132dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::ObjectArray<mirror::Object>* groups_array =
18142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>();
1815caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  const int32_t size = size_field->GetInt(groups_array_list);
1816caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes
1817caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Copy the first 'size' elements out of the array into the result.
1818caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  for (int32_t i = 0; i < size; ++i) {
1819caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    child_thread_group_ids.push_back(gRegistry->Add(groups_array->Get(i)));
1820caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  }
1821872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1822872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
182300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic int GetStackDepth(Thread* thread)
1824b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
18250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct CountStackDepthVisitor : public StackVisitor {
182693ba893c20532990a430741e0a97212900094e8cBrian Carlstrom    explicit CountStackDepthVisitor(Thread* thread)
18277a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth(0) {}
18280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
182964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
183064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
183164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
18320399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!GetMethod()->IsRuntimeMethod()) {
1833f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes        ++depth;
1834f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes      }
1835530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
1836a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1837a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    size_t depth;
1838a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  };
183908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
18407a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  CountStackDepthVisitor visitor(thread);
18410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
1842a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return visitor.depth;
1843872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1844872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1845221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result) {
184600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1847a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1848221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1849221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1850221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1851221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1852221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1853f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
1854f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
1855f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
1856221229cb523f849f165fdafbf9785010963715daElliott Hughes  result = GetStackDepth(thread);
1857221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
18588696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
18598696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
1860306057fd278d75bf3794bd5243a3b6652c487d18Ian RogersJDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame,
1861306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers                                     size_t frame_count, JDWP::ExpandBuf* buf) {
18626e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  class GetFrameVisitor : public StackVisitor {
18636e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   public:
18647a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    GetFrameVisitor(Thread* thread, size_t start_frame, size_t frame_count, JDWP::ExpandBuf* buf)
1865b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
18667a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth_(0),
18676e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes          start_frame_(start_frame), frame_count_(frame_count), buf_(buf) {
18686e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      expandBufAdd4BE(buf_, frame_count_);
186903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
18700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
187100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
187200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
187300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
18740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetMethod()->IsRuntimeMethod()) {
18757934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        return true;  // The debugger can't do anything useful with a frame that has no Method*.
187603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
18776e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_ + frame_count_) {
1878530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes        return false;
187903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
18806e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_) {
18816e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::FrameId frame_id(GetFrameId());
18826e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::JdwpLocation location;
18836e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        SetLocation(location, GetMethod(), GetDexPc());
18847baf96f592522b763d15389a01c6ab6f0a977621Elliott Hughes        VLOG(jdwp) << StringPrintf("    Frame %3zd: id=%3lld ", depth_, frame_id) << location;
18856e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAdd8BE(buf_, frame_id);
18866e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAddLocation(buf_, location);
18876e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      }
18886e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      ++depth_;
1889530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
189003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
18916e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes
18926e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   private:
18936e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    size_t depth_;
18946e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t start_frame_;
18956e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t frame_count_;
18966e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    JDWP::ExpandBuf* buf_;
189703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
189800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
189900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
1900a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1901221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1902221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1903221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1904221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1905221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1906f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
1907f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
1908f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
19097a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetFrameVisitor visitor(thread, start_frame, frame_count, buf);
19100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
19116e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
1912872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1913872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1914872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetThreadSelfId() {
1915dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier  ScopedObjectAccessUnchecked soa(Thread::Current());
1916cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  return gRegistry->Add(soa.Self()->GetPeer());
1917872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1918872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1919475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::SuspendVM() {
192000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
1921872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1922872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1923872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ResumeVM() {
1924c61a267e98ed1038b74c33c7740414ced4a27d89Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
1925872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1926872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1927221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) {
192800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedLocalRef<jobject> peer(Thread::Current()->GetJniEnv(), NULL);
192900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
193000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
19312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id)));
193200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
193300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (peer.get() == NULL) {
193400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
193500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
193600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Suspend thread to build stack trace.
1937f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  bool timed_out;
1938f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Thread* thread = Thread::SuspendForDebugger(peer.get(), request_suspension, &timed_out);
193900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (thread != NULL) {
194000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_NONE;
1941f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else if (timed_out) {
194200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_INTERNAL;
194300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  } else {
194400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
19454e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
1946872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1947872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1948221229cb523f849f165fdafbf9785010963715daElliott Hughesvoid Dbg::ResumeThread(JDWP::ObjectId thread_id) {
194900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
19502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id);
1951a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  Thread* thread;
1952a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  {
1953a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1954a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    thread = Thread::FromManagedThread(soa, peer);
1955a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  }
19564e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  if (thread == NULL) {
19574e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    LOG(WARNING) << "No such thread for resume: " << peer;
19584e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    return;
19594e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
196000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool needs_resume;
196100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
196250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
196300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    needs_resume = thread->GetSuspendCount() > 0;
196400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
196500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (needs_resume) {
1966546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    Runtime::Current()->GetThreadList()->Resume(thread, true);
1967546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
1968872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1969872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1970872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::SuspendSelf() {
1971475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
1972872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1973872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
19740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct GetThisVisitor : public StackVisitor {
19757a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
1976b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
19777a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, context), this_object(NULL), frame_id(frame_id) {}
197868fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
197900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
198000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
198100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
19826e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    if (frame_id != GetFrameId()) {
19830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return true;  // continue
19840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    } else {
198562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      this_object = GetThisObject();
198662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return false;
19870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
198868fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  }
1989dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
19902dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* this_object;
19916e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  JDWP::FrameId frame_id;
19920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers};
1993ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
199400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersJDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id,
199500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ObjectId* result) {
199600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
199700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* thread;
199800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
199950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2000221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2001221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
2002221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
200300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
20049e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    if (!IsSuspendedForDebugger(soa, thread)) {
200500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      return JDWP::ERR_THREAD_NOT_SUSPENDED;
200600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
20076e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  }
2008caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  UniquePtr<Context> context(Context::Create());
20097a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor visitor(thread, context.get(), frame_id);
20100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
20116e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  *result = gRegistry->Add(visitor.this_object);
20126e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
20130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
20140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
201588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::GetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag,
201600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                        uint8_t* buf, size_t width) {
20170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct GetLocalVisitor : public StackVisitor {
20187a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    GetLocalVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id, int slot,
20197a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers                    JDWP::JdwpTag tag, uint8_t* buf, size_t width)
2020b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
20217a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context), frame_id_(frame_id), slot_(slot), tag_(tag),
2022ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers          buf_(buf), width_(width) {}
2023ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
202400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
202500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
202600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
20270399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetFrameId() != frame_id_) {
20280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        return true;  // Not our frame, carry on.
20290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
20300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // TODO: check that the tag is compatible with the actual type of the slot!
2031ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* m = GetMethod();
20320399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      uint16_t reg = DemangleSlot(slot_, m);
20330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
20340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      switch (tag_) {
20350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_BOOLEAN:
20360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
20370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
20382bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
20390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get boolean local " << reg << " = " << intVal;
20400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set1(buf_+1, intVal != 0);
20410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
20420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
20430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_BYTE:
20440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
20450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
20462bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
20470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get byte local " << reg << " = " << intVal;
20480399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set1(buf_+1, intVal);
20490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
20500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
20510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_SHORT:
20520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CHAR:
20530399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
20540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 2U);
20552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
20560399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get short/char local " << reg << " = " << intVal;
20570399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set2BE(buf_+1, intVal);
20580399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
20590399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
20600399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_INT:
20612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        {
20622bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 4U);
20632bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
20642bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          VLOG(jdwp) << "get int local " << reg << " = " << intVal;
20652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          JDWP::Set4BE(buf_+1, intVal);
20662bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
20672bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        break;
20680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_FLOAT:
20690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
20700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 4U);
20712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kFloatVReg);
20720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get int/float local " << reg << " = " << intVal;
20730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set4BE(buf_+1, intVal);
20740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
20750399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
20760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_ARRAY:
20770399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
20780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
20792dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
20800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get array local " << reg << " = " << o;
20810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
20820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            LOG(FATAL) << "Register " << reg << " expected to hold array: " << o;
20830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
20840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
20850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
20860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
20870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CLASS_LOADER:
20880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CLASS_OBJECT:
20890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_OBJECT:
20900399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_STRING:
20910399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_THREAD:
20920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_THREAD_GROUP:
20930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
20940399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
20952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
20960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get object local " << reg << " = " << o;
20970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          if (!Runtime::Current()->GetHeap()->IsHeapAddress(o)) {
20980399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            LOG(FATAL) << "Register " << reg << " expected to hold object: " << o;
20990399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
21000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          tag_ = TagFromObject(o);
21010399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
21020399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
21030399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
21040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_DOUBLE:
21052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        {
21062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 8U);
21072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t lo = GetVReg(m, reg, kDoubleLoVReg);
21082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t hi = GetVReg(m, reg + 1, kDoubleHiVReg);
21092bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t longVal = (hi << 32) | lo;
21102bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
21112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          JDWP::Set8BE(buf_+1, longVal);
21122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
21132bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        break;
21140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_LONG:
21150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
21160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 8U);
21172bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t lo = GetVReg(m, reg, kLongLoVReg);
21182bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t hi = GetVReg(m, reg + 1, kLongHiVReg);
21190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          uint64_t longVal = (hi << 32) | lo;
21200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
21210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set8BE(buf_+1, longVal);
21220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
21230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
21240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      default:
21250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        LOG(FATAL) << "Unknown tag " << tag_;
21260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
21270399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
2128ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
21290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // Prepend tag, which may have been updated.
21300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      JDWP::Set1(buf_, tag_);
21310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return false;
21320399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
21330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
21340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::FrameId frame_id_;
21350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const int slot_;
21360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    JDWP::JdwpTag tag_;
21370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    uint8_t* const buf_;
21380399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const size_t width_;
21390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
214000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
214100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2142a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2143221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2144221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2145221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2146221229cb523f849f165fdafbf9785010963715daElliott Hughes    return;
2147221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
21480399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  UniquePtr<Context> context(Context::Create());
21497a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, buf, width);
21500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
21510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
21520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
215388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::SetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag,
21540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                        uint64_t value, size_t width) {
21550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SetLocalVisitor : public StackVisitor {
21567a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    SetLocalVisitor(Thread* thread, Context* context,
21570399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                    JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint64_t value,
2158ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers                    size_t width)
2159b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
21607a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context),
216108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes          frame_id_(frame_id), slot_(slot), tag_(tag), value_(value), width_(width) {}
2162ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
216300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
216400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
216500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
21660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetFrameId() != frame_id_) {
21670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        return true;  // Not our frame, carry on.
2168ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes      }
21690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // TODO: check that the tag is compatible with the actual type of the slot!
2170ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* m = GetMethod();
21710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      uint16_t reg = DemangleSlot(slot_, m);
21720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
21730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      switch (tag_) {
21740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_BOOLEAN:
21750399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_BYTE:
21760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
21772bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
21780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
21790399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_SHORT:
21800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_CHAR:
21810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 2U);
21822bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
21830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
21840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_INT:
21852bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 4U);
21862bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
21872bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          break;
21880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_FLOAT:
21890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 4U);
21902bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kFloatVReg);
21910399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
21920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_ARRAY:
21930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_OBJECT:
21940399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_STRING:
21950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
21960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
21972dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value_));
219864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes          if (o == ObjectRegistry::kInvalidObject) {
21990399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            UNIMPLEMENTED(FATAL) << "return an error code when given an invalid object to store";
22000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
22012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), kReferenceVReg);
22020399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
22030399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
22040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_DOUBLE:
22052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 8U);
22062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kDoubleLoVReg);
22072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kDoubleHiVReg);
22082bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          break;
22090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_LONG:
22100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 8U);
22112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kLongLoVReg);
22122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kLongHiVReg);
22130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
22140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        default:
22150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          LOG(FATAL) << "Unknown tag " << tag_;
22160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
22170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
22180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return false;
2219cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    }
22200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
22210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::FrameId frame_id_;
22220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const int slot_;
22230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::JdwpTag tag_;
22240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const uint64_t value_;
22250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const size_t width_;
22260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
222700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
222800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2229a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2230221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2231221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2232221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2233221229cb523f849f165fdafbf9785010963715daElliott Hughes    return;
2234221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
223508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  UniquePtr<Context> context(Context::Create());
22367a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  SetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, value, width);
22370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2238872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2239872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2240ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromvoid Dbg::PostLocationEvent(const mirror::ArtMethod* m, int dex_pc,
224162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                            mirror::Object* this_object, int event_flags) {
22422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = m->GetDeclaringClass();
224391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
224491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  JDWP::JdwpLocation location;
2245748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes  location.type_tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
224664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  location.class_id = gRegistry->AddRefType(c);
2247748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes  location.method_id = ToMethodId(m);
2248972a47bf0c180dee7e4c697b74dd31daaf58e008Elliott Hughes  location.dex_pc = m->IsNative() ? -1 : dex_pc;
224991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
225064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // If 'this_object' isn't already in the registry, we know that we're not looking for it,
225164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // so there's no point adding it to the registry and burning through ids.
225264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::ObjectId this_id = 0;
225364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (gRegistry->Contains(this_object)) {
225464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    this_id = gRegistry->Add(this_object);
225591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
225664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gJdwpState->PostLocationEvent(&location, this_id, event_flags);
2257872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2258872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
225962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Dbg::PostException(Thread* thread, const ThrowLocation& throw_location,
2260ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                        mirror::ArtMethod* catch_method,
226164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes                        uint32_t catch_dex_pc, mirror::Throwable* exception_object) {
2262c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
22630ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers    return;
22640ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers  }
22654740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
226662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  JDWP::JdwpLocation jdwp_throw_location;
226762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SetLocation(jdwp_throw_location, throw_location.GetMethod(), throw_location.GetDexPc());
2268d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  JDWP::JdwpLocation catch_location;
2269caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  SetLocation(catch_location, catch_method, catch_dex_pc);
2270d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2271d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // We need 'this' for InstanceOnly filters.
227262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  JDWP::ObjectId this_id = gRegistry->Add(throw_location.GetThis());
227364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::ObjectId exception_id = gRegistry->Add(exception_object);
227464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId exception_class_id = gRegistry->AddRefType(exception_object->GetClass());
2275d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
227662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  gJdwpState->PostException(&jdwp_throw_location, exception_id, exception_class_id, &catch_location,
227762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                            this_id);
2278872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2279872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
22802dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::PostClassPrepare(mirror::Class* c) {
2281c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
22824740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes    return;
22834740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  }
22844740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
22853d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes  // OLD-TODO - we currently always send both "verified" and "prepared" since
22864740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  // debuggers seem to like that.  There might be some advantage to honesty,
22874740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  // since the class may not yet be verified.
22884740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
22894740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  JDWP::JdwpTypeTag tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
22904740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  gJdwpState->PostClassPrepare(tag, gRegistry->Add(c), ClassHelper(c).GetDescriptor(), state);
2291872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2292872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
229362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object,
2294ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                         const mirror::ArtMethod* m, uint32_t dex_pc) {
229562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) {
22962aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes    return;
229791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
229891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
22992aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes  int event_flags = 0;
23002aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes
23018696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  if (IsBreakpoint(m, dex_pc)) {
23028696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    event_flags |= kBreakpoint;
230391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
230491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
230509bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao  {
230609bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    // If the debugger is single-stepping one of our threads, check to
230709bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    // see if we're that thread and we've reached a step point.
230809bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
230962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (gSingleStepControl.is_active && gSingleStepControl.thread == thread) {
231009bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao      CHECK(!m->IsNative());
231109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao      if (gSingleStepControl.step_depth == JDWP::SD_INTO) {
231209bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // Step into method calls.  We break when the line number
231309bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // or method pointer changes.  If we're in SS_MIN mode, we
231409bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // always stop.
231509bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        if (gSingleStepControl.method != m) {
231609bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao          event_flags |= kSingleStep;
231709bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao          VLOG(jdwp) << "SS new method";
231809bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        } else if (gSingleStepControl.step_size == JDWP::SS_MIN) {
23198696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          event_flags |= kSingleStep;
23208696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          VLOG(jdwp) << "SS new instruction";
23212435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) {
23222435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          event_flags |= kSingleStep;
23232435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          VLOG(jdwp) << "SS new line";
232491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes        }
232509bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao      } else if (gSingleStepControl.step_depth == JDWP::SD_OVER) {
232609bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // Step over method calls.  We break when the line number is
232709bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // different and the frame depth is <= the original frame
232809bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // depth.  (We can't just compare on the method, because we
232909bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // might get unrolled past it by an exception, and it's tricky
233009bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // to identify recursion.)
233109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao
233262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        int stack_depth = GetStackDepth(thread);
233309bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao
233409bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        if (stack_depth < gSingleStepControl.stack_depth) {
233509bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao          // popped up one or more frames, always trigger
233609bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao          event_flags |= kSingleStep;
233709bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao          VLOG(jdwp) << "SS method pop";
233809bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        } else if (stack_depth == gSingleStepControl.stack_depth) {
233909bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao          // same depth, see if we moved
234009bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao          if (gSingleStepControl.step_size == JDWP::SS_MIN) {
234109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao            event_flags |= kSingleStep;
234209bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao            VLOG(jdwp) << "SS new instruction";
234309bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao          } else if (gSingleStepControl.dex_pcs.find(dex_pc) == gSingleStepControl.dex_pcs.end()) {
234409bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao            event_flags |= kSingleStep;
234509bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao            VLOG(jdwp) << "SS new line";
234609bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao          }
234709bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        }
234809bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao      } else {
234909bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        CHECK_EQ(gSingleStepControl.step_depth, JDWP::SD_OUT);
235009bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // Return from the current method.  We break when the frame
235109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // depth pops up.
235209bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao
235309bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // This differs from the "method exit" break in that it stops
235409bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // with the PC at the next instruction in the returned-to
235509bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        // function, rather than the end of the returning function.
235609bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao
235762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        int stack_depth = GetStackDepth(thread);
235809bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        if (stack_depth < gSingleStepControl.stack_depth) {
235909bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao          event_flags |= kSingleStep;
236009bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao          VLOG(jdwp) << "SS method pop";
236109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        }
23628696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
236391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    }
236491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
236591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
236691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // If there's something interesting going on, see if it matches one
236791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // of the debugger filters.
236891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (event_flags != 0) {
236962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags);
237091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
237191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes}
237291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
23738696433d1b3d8ba15288483b777edd888de69135Elliott Hughesvoid Dbg::WatchLocation(const JDWP::JdwpLocation* location) {
237409bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
2375ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(location->method_id);
2376972a47bf0c180dee7e4c697b74dd31daaf58e008Elliott Hughes  gBreakpoints.push_back(Breakpoint(m, location->dex_pc));
23778696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " << gBreakpoints[gBreakpoints.size() - 1];
2378872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2379872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
23808696433d1b3d8ba15288483b777edd888de69135Elliott Hughesvoid Dbg::UnwatchLocation(const JDWP::JdwpLocation* location) {
238109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
2382ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(location->method_id);
23838696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  for (size_t i = 0; i < gBreakpoints.size(); ++i) {
2384972a47bf0c180dee7e4c697b74dd31daaf58e008Elliott Hughes    if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == location->dex_pc) {
23858696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
23868696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      gBreakpoints.erase(gBreakpoints.begin() + i);
23878696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      return;
23888696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
23898696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
2390872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2391872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2392449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't
2393449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// cause suspension if the thread is the current thread.
2394449db33fafa29578df60e8a323f78d5eb6247e76Jeff Haoclass ScopedThreadSuspension {
2395449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao public:
239633e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers  ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id)
239733e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
2398449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      thread_(NULL),
2399449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_(JDWP::ERR_NONE),
2400449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      self_suspend_(false),
240133e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      other_suspend_(false) {
2402449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    ScopedObjectAccessUnchecked soa(self);
2403449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    {
2404449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2405449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_ = DecodeThread(soa, thread_id, thread_);
2406449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
2407449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (error_ == JDWP::ERR_NONE) {
2408449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      if (thread_ == soa.Self()) {
2409449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        self_suspend_ = true;
2410449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      } else {
2411449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
2412449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        jobject thread_peer = gRegistry->GetJObject(thread_id);
2413449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        bool timed_out;
2414449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        Thread* suspended_thread = Thread::SuspendForDebugger(thread_peer, true, &timed_out);
2415449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension);
2416449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        if (suspended_thread == NULL) {
2417449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          // Thread terminated from under us while suspending.
2418449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          error_ = JDWP::ERR_INVALID_THREAD;
2419449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        } else {
2420449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          CHECK_EQ(suspended_thread, thread_);
2421449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          other_suspend_ = true;
2422449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        }
2423449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      }
2424449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
2425449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2426449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2427449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* GetThread() const {
2428449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return thread_;
2429449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2430449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2431449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError GetError() const {
2432449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return error_;
2433449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2434449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2435449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ~ScopedThreadSuspension() {
2436449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (other_suspend_) {
2437449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      Runtime::Current()->GetThreadList()->Resume(thread_, true);
2438449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
2439449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2440449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2441449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao private:
2442449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* thread_;
2443449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError error_;
2444449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool self_suspend_;
2445449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool other_suspend_;
2446449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao};
2447449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2448221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size,
244900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::JdwpStepDepth step_depth) {
2450449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* self = Thread::Current();
2451449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ScopedThreadSuspension sts(self, thread_id);
2452449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  if (sts.GetError() != JDWP::ERR_NONE) {
2453449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return sts.GetError();
24542435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
24558696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
2456449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  MutexLock mu2(self, *Locks::breakpoint_lock_);
24578696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  // TODO: there's no theoretical reason why we couldn't support single-stepping
24588696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  // of multiple threads at once, but we never did so historically.
2459449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  if (gSingleStepControl.thread != NULL && sts.GetThread() != gSingleStepControl.thread) {
24608696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    LOG(WARNING) << "single-step already active for " << *gSingleStepControl.thread
2461449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao                 << "; switching to " << *sts.GetThread();
24628696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
24638696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
24642435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
24652435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Work out what Method* we're in, the current line number, and how deep the stack currently
24662435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // is for step-out.
24672435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
24682435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
24690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SingleStepStackVisitor : public StackVisitor {
247093ba893c20532990a430741e0a97212900094e8cBrian Carlstrom    explicit SingleStepStackVisitor(Thread* thread)
247109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao        EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_)
2472b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
24737a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL) {
24748696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      gSingleStepControl.method = NULL;
24758696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      gSingleStepControl.stack_depth = 0;
24768696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
2477ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
247800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
247900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
248000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
248109bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao      Locks::breakpoint_lock_->AssertHeld(Thread::Current());
2482ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      const mirror::ArtMethod* m = GetMethod();
24830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!m->IsRuntimeMethod()) {
24848696433d1b3d8ba15288483b777edd888de69135Elliott Hughes        ++gSingleStepControl.stack_depth;
24858696433d1b3d8ba15288483b777edd888de69135Elliott Hughes        if (gSingleStepControl.method == NULL) {
24862dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          const mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache();
24872435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          gSingleStepControl.method = m;
24882435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          gSingleStepControl.line_number = -1;
24892435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          if (dex_cache != NULL) {
24904445a7e3398a6143939168097a3aa275b734504dIan Rogers            const DexFile& dex_file = *dex_cache->GetDexFile();
24910399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            gSingleStepControl.line_number = dex_file.GetLineNumFromPC(m, GetDexPc());
24922435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          }
24938696433d1b3d8ba15288483b777edd888de69135Elliott Hughes        }
24948696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
2495530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
24968696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
24978696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  };
2498449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2499449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  SingleStepStackVisitor visitor(sts.GetThread());
25000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
25018696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
25022435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
25032435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Find the dex_pc values that correspond to the current line, for line-based single-stepping.
25042435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
25052435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
25062435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  struct DebugCallbackContext {
250709bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    DebugCallbackContext() EXCLUSIVE_LOCKS_REQUIRED(Locks::breakpoint_lock_) {
25082435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      last_pc_valid = false;
25092435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      last_pc = 0;
25102435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
25112435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
251209bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
251309bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    // annotalysis.
251409bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) NO_THREAD_SAFETY_ANALYSIS {
251509bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao      Locks::breakpoint_lock_->AssertHeld(Thread::Current());
25162435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context);
25172435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      if (static_cast<int32_t>(line_number) == gSingleStepControl.line_number) {
25182435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        if (!context->last_pc_valid) {
25192435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          // Everything from this address until the next line change is ours.
25202435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc = address;
25212435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc_valid = true;
25222435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
25232435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Otherwise, if we're already in a valid range for this line,
25242435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // just keep going (shouldn't really happen)...
25257934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      } else if (context->last_pc_valid) {  // and the line number is new
25262435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Add everything from the last entry up until here to the set
25272435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) {
25282435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          gSingleStepControl.dex_pcs.insert(dex_pc);
25292435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
25302435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        context->last_pc_valid = false;
25312435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
25327934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return false;  // There may be multiple entries for any given line.
25332435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
25342435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
253509bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
253609bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    // annotalysis.
253709bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    ~DebugCallbackContext() NO_THREAD_SAFETY_ANALYSIS {
253809bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao      Locks::breakpoint_lock_->AssertHeld(Thread::Current());
25392435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      // If the line number was the last in the position table...
25402435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      if (last_pc_valid) {
25412435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        size_t end = MethodHelper(gSingleStepControl.method).GetCodeItem()->insns_size_in_code_units_;
25422435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) {
25432435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          gSingleStepControl.dex_pcs.insert(dex_pc);
25442435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
25452435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
25462435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
25472435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
25482435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    bool last_pc_valid;
25492435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    uint32_t last_pc;
25502435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  };
25513e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes  gSingleStepControl.dex_pcs.clear();
2552ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  const mirror::ArtMethod* m = gSingleStepControl.method;
25533e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes  if (m->IsNative()) {
25543e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes    gSingleStepControl.line_number = -1;
25553e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes  } else {
25563e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes    DebugCallbackContext context;
25573e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes    MethodHelper mh(m);
25583e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes    mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(),
25593e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes                                    DebugCallbackContext::Callback, NULL, &context);
25603e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes  }
25612435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
25622435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
25632435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Everything else...
25642435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
25652435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
2566449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  gSingleStepControl.thread = sts.GetThread();
25678696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gSingleStepControl.step_size = step_size;
25688696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gSingleStepControl.step_depth = step_depth;
25698696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gSingleStepControl.is_active = true;
25708696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
25712435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  if (VLOG_IS_ON(jdwp)) {
25722435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step thread: " << *gSingleStepControl.thread;
25732435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step step size: " << gSingleStepControl.step_size;
25742435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step step depth: " << gSingleStepControl.step_depth;
25752435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step current method: " << PrettyMethod(gSingleStepControl.method);
25762435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step current line: " << gSingleStepControl.line_number;
25772435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step current stack depth: " << gSingleStepControl.stack_depth;
25782435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step dex_pc values:";
25792435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    for (std::set<uint32_t>::iterator it = gSingleStepControl.dex_pcs.begin() ; it != gSingleStepControl.dex_pcs.end(); ++it) {
2580229feb7a09317919ee51c06d1c3e715cea25da75Elliott Hughes      VLOG(jdwp) << StringPrintf(" %#x", *it);
25812435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
25822435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
25832435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
25842435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
2585872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2586872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2587221229cb523f849f165fdafbf9785010963715daElliott Hughesvoid Dbg::UnconfigureStep(JDWP::ObjectId /*thread_id*/) {
258809bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
2589f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughes
25908696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gSingleStepControl.is_active = false;
25918696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gSingleStepControl.thread = NULL;
25922435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  gSingleStepControl.dex_pcs.clear();
2593872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2594872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
259545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughesstatic char JdwpTagToShortyChar(JDWP::JdwpTag tag) {
259645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  switch (tag) {
259745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    default:
259845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag);
259945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
260045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Primitives.
260145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BYTE:    return 'B';
260245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CHAR:    return 'C';
260345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_FLOAT:   return 'F';
260445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_DOUBLE:  return 'D';
260545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_INT:     return 'I';
260645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_LONG:    return 'J';
260745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_SHORT:   return 'S';
260845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_VOID:    return 'V';
260945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BOOLEAN: return 'Z';
261045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
261145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Reference types.
261245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_ARRAY:
261345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_OBJECT:
261445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_STRING:
261545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD:
261645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD_GROUP:
261745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_LOADER:
261845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_OBJECT:
261945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return 'L';
262045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  }
262145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes}
262245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
262388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id,
262488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                  JDWP::RefTypeId class_id, JDWP::MethodId method_id,
262500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  uint32_t arg_count, uint64_t* arg_values,
262600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* arg_types, uint32_t options,
262700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* pResultTag, uint64_t* pResultValue,
262800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::ObjectId* pExceptionId) {
2629d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  ThreadList* thread_list = Runtime::Current()->GetThreadList();
2630d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2631d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  Thread* targetThread = NULL;
2632d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  DebugInvokeReq* req = NULL;
263300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* self = Thread::Current();
2634d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
263500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(self);
263650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2637221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, targetThread);
2638221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
2639221229cb523f849f165fdafbf9785010963715daElliott Hughes      LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id;
2640221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
2641d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
2642d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    req = targetThread->GetInvokeReq();
2643d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (!req->ready) {
2644d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread;
2645d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      return JDWP::ERR_INVALID_THREAD;
2646d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
2647d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2648d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
2649d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We currently have a bug where we don't successfully resume the
2650d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * target thread if the suspend count is too deep.  We're expected to
2651d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * require one "resume" for each "suspend", but when asked to execute
2652d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a method we have to resume fully and then re-suspend it back to the
2653d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * same level.  (The easiest way to cause this is to type "suspend"
2654d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * multiple times in jdb.)
2655d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
2656d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * It's unclear what this means when the event specifies "resume all"
2657d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * and some threads are suspended more deeply than others.  This is
2658d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a rare problem, so for now we just prevent it from hanging forever
2659d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * by rejecting the method invocation request.  Without this, we will
2660d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * be stuck waiting on a suspended thread.
2661d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
266200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    int suspend_count;
266300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
266450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
266500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      suspend_count = targetThread->GetSuspendCount();
266600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
2667d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (suspend_count > 1) {
2668d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count;
26697934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return JDWP::ERR_THREAD_SUSPENDED;  // Probably not expected here.
2670d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
2671d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
26723f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    JDWP::JdwpError status;
26732dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id);
267464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (receiver == ObjectRegistry::kInvalidObject) {
26753f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
26763f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
267745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
26782dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* thread = gRegistry->Get<mirror::Object*>(thread_id);
267964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (thread == ObjectRegistry::kInvalidObject) {
26803f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
26813f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
268245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // TODO: check that 'thread' is actually a java.lang.Thread!
268345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
26842dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_id, status);
268545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (c == NULL) {
26863f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return status;
26873f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
268845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
2689ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = FromMethodId(method_id);
269045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic() != (receiver == NULL)) {
269145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_INVALID_METHODID;
269245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
269345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic()) {
269445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (m->GetDeclaringClass() != c) {
269545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
269645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
269745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    } else {
269845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
269945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
270045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
270145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
270245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
270345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Check the argument list matches the method.
270445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    MethodHelper mh(m);
270545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (mh.GetShortyLength() - 1 != arg_count) {
270645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_ILLEGAL_ARGUMENT;
270745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
270845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    const char* shorty = mh.GetShorty();
27090920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes    const DexFile::TypeList* types = mh.GetParameterTypeList();
271045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    for (size_t i = 0; i < arg_count; ++i) {
271145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) {
271245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_ILLEGAL_ARGUMENT;
271345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
27140920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
27150920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      if (shorty[i + 1] == 'L') {
27160920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        // Did we really get an argument of an appropriate reference type?
27170920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        mirror::Class* parameter_type = mh.GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
27180920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i]);
27190920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        if (argument == ObjectRegistry::kInvalidObject) {
27200920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_INVALID_OBJECT;
27210920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
27220920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        if (!argument->InstanceOf(parameter_type)) {
27230920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_ILLEGAL_ARGUMENT;
27240920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
27250920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
27260920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        // Turn the on-the-wire ObjectId into a jobject.
27270920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]);
27280920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        v.l = gRegistry->GetJObject(arg_values[i]);
27290920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      }
273045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
273145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
273245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    req->receiver_ = receiver;
273345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    req->thread_ = thread;
273445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    req->class_ = c;
273545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    req->method_ = m;
273645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    req->arg_count_ = arg_count;
273745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    req->arg_values_ = arg_values;
2738d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    req->options_ = options;
2739d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    req->invoke_needed_ = true;
2740d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2741d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2742d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // The fact that we've released the thread list lock is a bit risky --- if the thread goes
2743d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // away we're sitting high and dry -- but we must release this before the ResumeAllThreads
2744d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // call, and it's unwise to hold it during WaitForSuspend.
2745d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2746d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
2747d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
2748d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We change our (JDWP thread) status, which should be THREAD_RUNNING,
274981ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes     * so we can suspend for a GC if the invoke request causes us to
2750d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * run out of memory.  It's also a good idea to change it before locking
2751d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the invokeReq mutex, although that should never be held for long.
2752d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
275300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
2754d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
27554dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Transferring control to event thread";
2756d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    {
275750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu(self, req->lock_);
2758d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2759d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
27604dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming all threads";
276100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers        thread_list->UndoDebuggerSuspensions();
2762d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      } else {
27634dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming event thread only";
2764d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes        thread_list->Resume(targetThread, true);
2765d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
2766d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2767d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      // Wait for the request to finish executing.
2768d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      while (req->invoke_needed_) {
2769c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers        req->cond_.Wait(self);
2770d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
2771d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
27724dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Control has returned from event thread";
2773d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2774d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* wait for thread to re-suspend itself */
2775df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom    SuspendThread(thread_id, false /* request_suspension */);
277600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
2777d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2778d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2779d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  /*
2780d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * Suspend the threads.  We waited for the target thread to suspend
2781d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * itself, so all we need to do is suspend the others.
2782d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   *
2783d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * The suspendAllThreads() call will double-suspend the event thread,
2784d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * so we want to resume the target thread once to keep the books straight.
2785d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   */
2786d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
278700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
27884dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Suspending all threads";
278900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    thread_list->SuspendAllForDebugger();
279000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
27914dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Resuming event thread to balance the count";
2792d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    thread_list->Resume(targetThread, true);
2793d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2794d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2795d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Copy the result.
2796d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pResultTag = req->result_tag;
2797d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (IsPrimitiveTag(req->result_tag)) {
2798f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = req->result_value.GetJ();
2799d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else {
2800f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = gRegistry->Add(req->result_value.GetL());
2801d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2802d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pExceptionId = req->exception;
2803d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  return req->error;
2804872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2805872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2806872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
280700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
2808d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
280981ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // We can be called while an exception is pending. We need
2810d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // to preserve that across the method invocation.
281162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SirtRef<mirror::Object> old_throw_this_object(soa.Self(), NULL);
2812ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), NULL);
281362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SirtRef<mirror::Throwable> old_exception(soa.Self(), NULL);
281462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  uint32_t old_throw_dex_pc;
281562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  {
281662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation old_throw_location;
281762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
281862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_this_object.reset(old_throw_location.GetThis());
281962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_method.reset(old_throw_location.GetMethod());
282062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_exception.reset(old_exception_obj);
282162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_dex_pc = old_throw_location.GetDexPc();
282262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->ClearException();
282362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
2824d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2825d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Translate the method through the vtable, unless the debugger wants to suppress it.
2826ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = pReq->method_;
2827d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if ((pReq->options_ & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver_ != NULL) {
2828ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* actual_method = pReq->class_->FindVirtualMethodForVirtualOrInterface(pReq->method_);
282945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (actual_method != m) {
283045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) << " to " << PrettyMethod(actual_method);
283145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      m = actual_method;
283245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
2833d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2834cfa9cfa3a94b33d300e1b08b48b3cb72de343dbdElliott Hughes  VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m)
2835cfa9cfa3a94b33d300e1b08b48b3cb72de343dbdElliott Hughes             << " receiver=" << pReq->receiver_
2836cfa9cfa3a94b33d300e1b08b48b3cb72de343dbdElliott Hughes             << " arg_count=" << pReq->arg_count_;
2837d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  CHECK(m != NULL);
2838d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2839d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  CHECK_EQ(sizeof(jvalue), sizeof(uint64_t));
2840d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
28415d9173014c1ca09f7249a6b07629aa37778b5f8fJeff Hao  MethodHelper mh(m);
28425d9173014c1ca09f7249a6b07629aa37778b5f8fJeff Hao  ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
28435d9173014c1ca09f7249a6b07629aa37778b5f8fJeff Hao  arg_array.BuildArgArray(soa, pReq->receiver_, reinterpret_cast<jvalue*>(pReq->arg_values_));
28446474d190d5604898354ebf767f1944b6e3e9b445Jeff Hao  InvokeWithArgArray(soa, m, &arg_array, &pReq->result_value, mh.GetShorty()[0]);
2845d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
284662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  mirror::Throwable* exception = soa.Self()->GetException(NULL);
284762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->ClearException();
284862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  pReq->exception = gRegistry->Add(exception);
2849d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  pReq->result_tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty());
2850d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (pReq->exception != 0) {
285162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    VLOG(jdwp) << "  JDWP invocation returning with exception=" << exception
285262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        << " " << exception->Dump();
2853f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    pReq->result_value.SetJ(0);
2854d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else if (pReq->result_tag == JDWP::JT_OBJECT) {
2855d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* if no exception thrown, examine object result more closely */
2856f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    JDWP::JdwpTag new_tag = TagFromObject(pReq->result_value.GetL());
2857d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (new_tag != pReq->result_tag) {
28584dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes      VLOG(jdwp) << "  JDWP promoted result from " << pReq->result_tag << " to " << new_tag;
2859d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      pReq->result_tag = new_tag;
2860d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
2861d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2862d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
2863d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * Register the object.  We don't actually need an ObjectId yet,
2864d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * but we do need to be sure that the GC won't move or discard the
2865d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * object when we switch out of RUNNING.  The ObjectId conversion
2866d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * will add the object to the "do not touch" list.
2867d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
2868d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We can't use the "tracked allocation" mechanism here because
2869d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the object is going to be handed off to a different thread.
2870d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
2871f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    gRegistry->Add(pReq->result_value.GetL());
2872d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2873d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2874d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (old_exception.get() != NULL) {
287562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
287662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                         old_throw_dex_pc);
287762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
2878d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2879872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2880872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2881d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes/*
28824b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * "request" contains a full JDWP packet, possibly with multiple chunks.  We
2883f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * need to process each, accumulate the replies, and ship the whole thing
2884f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * back.
2885f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
2886f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * Returns "true" if we have a reply.  The reply buffer is newly allocated,
2887f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * and includes the chunk type/length, followed by the data.
2888f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
28893d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes * OLD-TODO: we currently assume that the request and reply include a single
2890f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * chunk.  If this becomes inconvenient we will need to adapt.
2891f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes */
28924b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesbool Dbg::DdmHandlePacket(JDWP::Request& request, uint8_t** pReplyBuf, int* pReplyLen) {
2893f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  Thread* self = Thread::Current();
2894f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  JNIEnv* env = self->GetJniEnv();
2895f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
28964b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t type = request.ReadUnsigned32("type");
28974b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t length = request.ReadUnsigned32("length");
28984b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
28994b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  // Create a byte[] corresponding to 'request'.
29004b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  size_t request_length = request.size();
29014b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length));
29026a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (dataArray.get() == NULL) {
29034b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    LOG(WARNING) << "byte[] allocation failed: " << request_length;
2904f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
2905f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
2906f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
29074b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  env->SetByteArrayRegion(dataArray.get(), 0, request_length, reinterpret_cast<const jbyte*>(request.data()));
29084b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  request.Skip(request_length);
2909f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
2910f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Run through and find all chunks.  [Currently just find the first.]
29116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ScopedByteArrayRO contents(env, dataArray.get());
29124b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (length != request_length) {
29134b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, request_length);
2914f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
2915f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
2916f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
2917f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
2918eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
2919eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                                                                 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
29204b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                                                 type, dataArray.get(), 0, length));
2921f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (env->ExceptionCheck()) {
2922f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
2923f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionDescribe();
2924f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
2925f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
2926f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
2927f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
29286a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (chunk.get() == NULL) {
2929f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
2930f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
2931f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
2932f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  /*
2933f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * Pull the pieces out of the chunk.  We copy the results into a
2934f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * newly-allocated buffer that the caller can free.  We don't want to
2935f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * continue using the Chunk object because nothing has a reference to it.
2936f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
2937f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * We could avoid this by returning type/data/offset/length and having
2938f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * the caller be aware of the object lifetime issues, but that
293981ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes   * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
2940f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * if we have responses for multiple chunks.
2941f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
2942f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * So we're pretty much stuck with copying data around multiple times.
2943f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   */
2944eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
29454b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
2946eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
2947eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
2948f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
29494dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length);
29506a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (length == 0 || replyData.get() == NULL) {
2951f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
2952f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
2953f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
29544b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  const int kChunkHdrLen = 8;
2955f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  uint8_t* reply = new uint8_t[length + kChunkHdrLen];
2956f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (reply == NULL) {
2957f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
2958f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
2959f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
2960f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 0, type);
2961f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 4, length);
29626a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
2963f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
2964f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyBuf = reply;
2965f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyLen = length + kChunkHdrLen;
2966f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
29674b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length);
2968f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  return true;
2969872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2970872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2971a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::DdmBroadcast(bool connect) {
29724dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
297347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
297447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  Thread* self = Thread::Current();
297550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  if (self->GetState() != kRunnable) {
297650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
297750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    /* try anyway? */
297847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
297947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
298047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  JNIEnv* env = self->GetJniEnv();
298147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
2982eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
2983eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
2984eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            event);
298547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (env->ExceptionCheck()) {
298647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
298747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionDescribe();
298847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionClear();
298947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
299047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
299147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
2992872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmConnected() {
2993a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(true);
2994872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2995872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2996872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmDisconnected() {
2997a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(false);
299847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = false;
299947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
300047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
300147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes/*
30028218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes * Send a notification when a thread starts, stops, or changes its name.
300347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes *
300447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * Because we broadcast the full set of threads when the notifications are
300547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * first enabled, it's possible for "thread" to be actively executing.
300647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes */
30078218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
300847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (!gDdmThreadNotification) {
300947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    return;
301047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
301147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
30128218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  if (type == CHUNK_TYPE("THDE")) {
30138218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    uint8_t buf[4];
30148218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    JDWP::Set4BE(&buf[0], t->GetThinLockId());
30158218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
30168218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  } else {
30178218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
301800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
30192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    SirtRef<mirror::String> name(soa.Self(), t->GetThreadName(soa));
302047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    size_t char_count = (name.get() != NULL) ? name->GetLength() : 0;
3021725a957985171d712d5c048cc3d00ff14968784bjeffhao    const jchar* chars = (name.get() != NULL) ? name->GetCharArray()->GetData() : NULL;
302247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
302321f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    std::vector<uint8_t> bytes;
3024545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    JDWP::Append4BE(bytes, t->GetThinLockId());
3025545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    JDWP::AppendUtf16BE(bytes, chars, char_count);
302621f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
302721f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    Dbg::DdmSendChunk(type, bytes);
302847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
302947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
303047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
303147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::DdmSetThreadNotification(bool enable) {
303200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Enable/disable thread notifications.
303347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = enable;
303447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (enable) {
303500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Suspend the VM then post thread start notifications for all threads. Threads attaching will
303600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // see a suspension in progress and block until that ends. They then post their own start
303700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // notification.
303800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    SuspendVM();
303900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    std::list<Thread*> threads;
304050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
304100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
304250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu(self, *Locks::thread_list_lock_);
304300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      threads = Runtime::Current()->GetThreadList()->GetList();
304400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
304500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
304650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      ScopedObjectAccess soa(self);
304702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (Thread* thread : threads) {
304802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR"));
304900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
305000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
305100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ResumeVM();
305247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
305347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
305447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3055a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
3056c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (IsDebuggerActive()) {
3057dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier    ScopedObjectAccessUnchecked soa(Thread::Current());
3058cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers    JDWP::ObjectId id = gRegistry->Add(t->GetPeer());
30598218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR"));
306047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
30618218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  Dbg::DdmSendThreadNotification(t, type);
306247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
306347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
306447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadStart(Thread* t) {
3065a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
306647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
306747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
306847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadDeath(Thread* t) {
3069a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
3070872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3071872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
30728218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) {
30733bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(buf != NULL);
30743bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  iovec vec[1];
30753bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
30763bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_len = byte_count;
30773bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Dbg::DdmSendChunkV(type, vec, 1);
3078872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3079872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
308021f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) {
308121f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  DdmSendChunk(type, bytes.size(), &bytes[0]);
308221f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes}
308321f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes
3084f52935278fca8c7aa220543eef4544e3d1105d91Brian Carlstromvoid Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
30853bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (gJdwpState == NULL) {
30864dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type;
30873bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
3088cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    gJdwpState->DdmSendChunkV(type, iov, iov_count);
30893bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
3090872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3091872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3092767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesint Dbg::DdmHandleHpifChunk(HpifWhen when) {
3093767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when == HPIF_WHEN_NOW) {
30947162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(when);
3095767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return true;
3096767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3097767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3098767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
3099767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
3100767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3101767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3102767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3103767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  gDdmHpifWhen = when;
3104767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
3105767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3106767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3107767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesbool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
3108767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
3109767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
3110767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3111767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3112767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3113767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
3114767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
3115767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3116767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3117767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3118767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (native) {
3119767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhen = when;
3120767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhat = what;
3121767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  } else {
3122767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhen = when;
3123767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhat = what;
3124767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3125767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
3126767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3127767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
31287162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesvoid Dbg::DdmSendHeapInfo(HpifWhen reason) {
31297162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  // If there's a one-shot 'when', reset it.
31307162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  if (reason == gDdmHpifWhen) {
31317162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
31327162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes      gDdmHpifWhen = HPIF_WHEN_NEVER;
31337162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    }
31347162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  }
31357162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes
31367162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  /*
31377162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Chunk HPIF (client --> server)
31387162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
31397162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Heap Info. General information about the heap,
31407162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * suitable for a summary display.
31417162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
31427162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   [u4]: number of heaps
31437162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
31447162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   For each heap:
31457162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: heap ID
31467162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u8]: timestamp in ms since Unix epoch
31477162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u1]: capture reason (same as 'when' value from server)
31487162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: max heap size in bytes (-Xmx)
31497162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current heap size in bytes
31507162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of bytes allocated
31517162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of objects allocated
31527162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   */
31537162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  uint8_t heap_count = 1;
31541d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::Heap* heap = Runtime::Current()->GetHeap();
315521f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  std::vector<uint8_t> bytes;
3156545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append4BE(bytes, heap_count);
31577934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, 1);  // Heap id (bogus; we only have one heap).
3158545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append8BE(bytes, MilliTime());
3159545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append1BE(bytes, reason);
31607934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetMaxMemory());  // Max allowed heap size in bytes.
31617934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetTotalMemory());  // Current heap size in bytes.
3162b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetBytesAllocated());
3163b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetObjectsAllocated());
316421f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
316521f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes);
3166767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3167767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
31686a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgSolidity {
31696a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FREE = 0,
31706a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_HARD = 1,
31716a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SOFT = 2,
31726a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_WEAK = 3,
31736a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_PHANTOM = 4,
31746a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FINALIZABLE = 5,
31756a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SWEEP = 6,
31766a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
31776a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
31786a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgKind {
31796a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_OBJECT = 0,
31806a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_CLASS_OBJECT = 1,
31816a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_1 = 2,
31826a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_2 = 3,
31836a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_4 = 4,
31846a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_8 = 5,
31856a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_UNKNOWN = 6,
31866a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_NATIVE = 7,
31876a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
31886a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
31896a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_PARTIAL (1<<7)
31906a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
31916a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
319230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogersclass HeapChunkContext {
319330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers public:
31946a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Maximum chunk size.  Obtain this from the formula:
31956a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
31966a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  HeapChunkContext(bool merge, bool native)
319730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      : buf_(16384 - 16),
319830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        type_(0),
319930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        merge_(merge) {
32006a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
32016a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    if (native) {
320230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = CHUNK_TYPE("NHSG");
32036a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    } else {
320430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
32056a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
32066a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
32076a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
32086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ~HeapChunkContext() {
320930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (p_ > &buf_[0]) {
32106a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      Flush();
32116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
32126a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
32136a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
32146a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void EnsureHeader(const void* chunk_ptr) {
321530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (!needHeader_) {
32166a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      return;
32176a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
32186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
32196a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Start a new HPSx chunk.
32207934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 1);  // Heap id (bogus; we only have one heap).
32217934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write1BE(&p_, 8);  // Size of allocation unit, in bytes.
32226a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
32237934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr));  // virtual address of segment start.
32247934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 0);  // offset of this piece (relative to the virtual address).
32256a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // [u4]: length of piece, in allocation units
32266a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // We won't know this until we're done, so save the offset and stuff in a dummy value.
322730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = p_;
322830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Write4BE(&p_, 0x55555555);
322930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = false;
32306a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
32316a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3232b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3233d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    if (pieceLenField_ == NULL) {
3234d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
3235d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      CHECK(needHeader_);
3236d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      return;
3237d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    }
32386a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Patch the "length of piece" field.
323930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(&buf_[0], pieceLenField_);
324030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(pieceLenField_, p_);
324130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Set4BE(pieceLenField_, totalAllocationUnits_);
32426a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
324330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]);
32446a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
32456a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
32466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
324700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg)
3248b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3249b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
325030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes);
3251a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
3252a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
32536a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes private:
3254a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  enum { ALLOCATION_UNIT_SIZE = 8 };
3255a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
32566a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void Reset() {
325730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    p_ = &buf_[0];
325815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    startOfNextMemoryChunk_ = NULL;
325930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    totalAllocationUnits_ = 0;
326030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = true;
326130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = NULL;
32626a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
32636a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
326400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void HeapChunkCallback(void* start, void* /*end*/, size_t used_bytes)
3265b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3266b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
326730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
326830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // in the following code not to allocate memory, by ensuring buf_ is of the correct size
326915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (used_bytes == 0) {
327015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start == NULL) {
327115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            // Reset for start of new heap.
327215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
327315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
327415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
327515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Only process in use memory so that free region information
327615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // also includes dlmalloc book keeping.
3277a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
3278a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
32796a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
328015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    /* If we're looking at the native heap, we'll just return
328115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     * (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks
328215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     */
328315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bool native = type_ == CHUNK_TYPE("NHSG");
328415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
328515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (startOfNextMemoryChunk_ != NULL) {
328615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Transmit any pending free memory. Native free memory of
328715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // over kMaxFreeLen could be because of the use of mmaps, so
328815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // don't report. If not free memory then start a new segment.
328915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        bool flush = true;
329015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start > startOfNextMemoryChunk_) {
329115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            const size_t kMaxFreeLen = 2 * kPageSize;
329215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeStart = startOfNextMemoryChunk_;
329315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeEnd = start;
32942d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom            size_t freeLen = reinterpret_cast<char*>(freeEnd) - reinterpret_cast<char*>(freeStart);
329515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            if (!native || freeLen < kMaxFreeLen) {
329615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), freeStart, freeLen);
329715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                flush = false;
329815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            }
329915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
330015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (flush) {
330115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
330215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
330315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
330415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
33052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    const mirror::Object* obj = reinterpret_cast<const mirror::Object*>(start);
3306a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
3307a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Determine the type of this chunk.
3308a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
3309a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If it's the same, we should combine them.
331015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    uint8_t state = ExamineObject(obj, native);
331115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
331215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // allocation then the first sizeof(size_t) may belong to it.
331315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    const size_t dlMallocOverhead = sizeof(size_t);
331415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    AppendChunk(state, start, used_bytes + dlMallocOverhead);
33152d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom    startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + dlMallocOverhead;
331615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  }
331715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
331815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void AppendChunk(uint8_t state, void* ptr, size_t length)
3319b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
332015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // Make sure there's enough room left in the buffer.
332115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
332215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // 17 bytes for any header.
332315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t needed = (((length/ALLOCATION_UNIT_SIZE + 255) / 256) * 2) + 17;
332415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
332515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
332615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      Flush();
332715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
3328a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
332915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
333015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
333115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
333215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers          << needed << " bytes)";
333315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return;
333415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
333515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    EnsureHeader(ptr);
3336a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Write out the chunk description.
333715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    length /= ALLOCATION_UNIT_SIZE;   // Convert to allocation units.
333815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    totalAllocationUnits_ += length;
333915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    while (length > 256) {
334030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = state | HPSG_PARTIAL;
334130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = 255;     // length - 1
334215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      length -= 256;
3343a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
334430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    *p_++ = state;
334515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    *p_++ = length - 1;
33466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
33476a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
33482dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  uint8_t ExamineObject(const mirror::Object* o, bool is_native_heap)
3349b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
3350a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (o == NULL) {
3351a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_FREE, 0);
3352a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
33536a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3354a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // It's an allocated chunk. Figure out what it is.
33556a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3356a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If we're looking at the native heap, we'll just return
3357a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks.
335800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (is_native_heap) {
3359a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
33606a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
33616a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
33625bfa60ffcc953340feb711ed05cf576ac821905eIan Rogers    if (!Runtime::Current()->GetHeap()->IsLiveObjectLocked(o)) {
336315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
336400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
336500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
33662dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = o->GetClass();
3367a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c == NULL) {
3368a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      // The object was probably just created but hasn't been initialized yet.
3369a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
3370a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
33716a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3372b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes    if (!Runtime::Current()->GetHeap()->IsHeapAddress(c)) {
337315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
3374a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
3375a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
33766a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3377a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsClassClass()) {
3378a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
33796a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
33806a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3381a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsArrayClass()) {
3382a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (o->IsObjectArray()) {
3383a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3384a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
3385a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      switch (c->GetComponentSize()) {
3386a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
3387a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
3388a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3389a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
3390a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
33916a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
33926a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3393a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
33946a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
33956a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
339630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  std::vector<uint8_t> buf_;
339730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* p_;
339830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* pieceLenField_;
339915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void* startOfNextMemoryChunk_;
340030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  size_t totalAllocationUnits_;
340130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint32_t type_;
340230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool merge_;
340330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool needHeader_;
340430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers
3405a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
3406a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes};
34076a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
34086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesvoid Dbg::DdmSendHeapSegments(bool native) {
34096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhen when;
34106a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhat what;
34116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (!native) {
34126a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmHpsgWhen;
34136a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmHpsgWhat;
34146a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  } else {
34156a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmNhsgWhen;
34166a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmNhsgWhat;
34176a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
34186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (when == HPSG_WHEN_NEVER) {
34196a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    return;
34206a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
34216a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
34226a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Figure out what kind of chunks we'll be sending.
34236a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what);
34246a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
34256a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // First, send a heap start chunk.
34266a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  uint8_t heap_id[4];
34277934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Set4BE(&heap_id[0], 1);  // Heap id (bogus; we only have one heap).
34286a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id);
34296a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
34306a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Send a series of heap segment chunks.
3431a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native);
3432a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (native) {
34331d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context);
3434a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
34351d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
34361d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
343750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
343862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
34391d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    typedef std::vector<gc::space::ContinuousSpace*>::const_iterator It;
34401d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    for (It cur = spaces.begin(), end = spaces.end(); cur != end; ++cur) {
34411d54e73444e017d3a65234e0f193846f3e27472bIan Rogers      if ((*cur)->IsDlMallocSpace()) {
34421d54e73444e017d3a65234e0f193846f3e27472bIan Rogers        (*cur)->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
3443b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier      }
3444b062fdd4cb097fbae69b4bcb479c34d83ecab8caMathieu Chartier    }
3445e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    // Walk the large objects, these are not in the AllocSpace.
3446e0f0cb3d855cb5e926452b5e1ec8457adc4e454eMathieu Chartier    heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkCallback, &context);
3447a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
34486a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
34496a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Finally, send a heap end chunk.
34506a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id);
3451767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3452767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3453b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughesstatic size_t GetAllocTrackerMax() {
3454b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#ifdef HAVE_ANDROID_OS
3455b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  // Check whether there's a system property overriding the number of records.
3456b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  const char* propertyName = "dalvik.vm.allocTrackerMax";
3457b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  char allocRecordMaxString[PROPERTY_VALUE_MAX];
3458b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  if (property_get(propertyName, allocRecordMaxString, "") > 0) {
3459b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    char* end;
3460b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    size_t value = strtoul(allocRecordMaxString, &end, 10);
3461b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (*end != '\0') {
34623e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
34633e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- invalid";
3464b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
3465b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
3466b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (!IsPowerOfTwo(value)) {
34673e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
34683e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- not power of two";
3469b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
3470b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
3471b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    return value;
3472b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  }
3473b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#endif
3474b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  return kDefaultNumAllocRecords;
3475b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes}
3476b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes
3477545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::SetAllocTrackingEnabled(bool enabled) {
347850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), gAllocTrackerLock);
3479545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (enabled) {
3480545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if (recent_allocation_records_ == NULL) {
3481b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      gAllocRecordMax = GetAllocTrackerMax();
3482b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      LOG(INFO) << "Enabling alloc tracker (" << gAllocRecordMax << " entries of "
3483b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes                << kMaxAllocRecordStackDepth << " frames, taking "
3484b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes                << PrettySize(sizeof(AllocRecord) * gAllocRecordMax) << ")";
3485545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      gAllocRecordHead = gAllocRecordCount = 0;
3486b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      recent_allocation_records_ = new AllocRecord[gAllocRecordMax];
3487545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      CHECK(recent_allocation_records_ != NULL);
3488545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3489545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  } else {
3490545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    delete[] recent_allocation_records_;
3491545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    recent_allocation_records_ = NULL;
3492545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3493545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
3494545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
34950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct AllocRecordStackVisitor : public StackVisitor {
34967a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor(Thread* thread, AllocRecord* record)
3497b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
34987a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, NULL), record(record), depth(0) {}
3499545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
350000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
350100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
350200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
3503545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if (depth >= kMaxAllocRecordStackDepth) {
3504530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return false;
3505545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3506ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = GetMethod();
35070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    if (!m->IsRuntimeMethod()) {
35080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].method = m;
35090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].dex_pc = GetDexPc();
3510530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      ++depth;
3511545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3512530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes    return true;
3513545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3514545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3515545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  ~AllocRecordStackVisitor() {
3516545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // Clear out any unused stack trace elements.
3517545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (; depth < kMaxAllocRecordStackDepth; ++depth) {
3518545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      record->stack[depth].method = NULL;
35190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].dex_pc = 0;
3520545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3521545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3522545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3523545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  AllocRecord* record;
3524545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t depth;
3525545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
3526545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
35272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::RecordAllocation(mirror::Class* type, size_t byte_count) {
3528545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  Thread* self = Thread::Current();
3529545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  CHECK(self != NULL);
3530545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
353150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(self, gAllocTrackerLock);
3532545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
3533545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
3534545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3535545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3536545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Advance and clip.
3537b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  if (++gAllocRecordHead == gAllocRecordMax) {
3538545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    gAllocRecordHead = 0;
3539545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3540545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3541545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the basics.
3542545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  AllocRecord* record = &recent_allocation_records_[gAllocRecordHead];
3543545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  record->type = type;
3544545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  record->byte_count = byte_count;
3545545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  record->thin_lock_id = self->GetThinLockId();
3546545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3547545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the stack trace.
35487a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor visitor(self, record);
35490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
3550545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3551b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  if (gAllocRecordCount < gAllocRecordMax) {
3552545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    ++gAllocRecordCount;
3553545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3554545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
3555545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3556a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// Returns the index of the head element.
3557a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
3558a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We point at the most-recently-written record, so if gAllocRecordCount is 1
3559a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// we want to use the current element.  Take "head+1" and subtract count
3560a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// from it.
3561a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
3562a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We need to handle underflow in our circular buffer, so we add
3563b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes// gAllocRecordMax and then mask it back down.
3564f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughesstatic inline int HeadIndex() EXCLUSIVE_LOCKS_REQUIRED(gAllocTrackerLock) {
3565b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  return (gAllocRecordHead+1 + gAllocRecordMax - gAllocRecordCount) & (gAllocRecordMax-1);
3566545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
3567545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3568545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::DumpRecentAllocations() {
356900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
357050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), gAllocTrackerLock);
3571545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
3572545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    LOG(INFO) << "Not recording tracked allocations";
3573545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
3574545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3575545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3576545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // "i" is the head of the list.  We want to start at the end of the
3577545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // list and move forward to the tail.
3578a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t i = HeadIndex();
3579545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t count = gAllocRecordCount;
3580545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3581545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  LOG(INFO) << "Tracked allocations, (head=" << gAllocRecordHead << " count=" << count << ")";
3582545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  while (count--) {
3583545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    AllocRecord* record = &recent_allocation_records_[i];
3584545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3585a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->thin_lock_id, record->byte_count)
3586545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes              << PrettyClass(record->type);
3587545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3588545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
3589ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      const mirror::ArtMethod* m = record->stack[stack_frame].method;
3590545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      if (m == NULL) {
3591545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes        break;
3592545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
3593545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      LOG(INFO) << "    " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber();
3594545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3595545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3596545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // pause periodically to help logcat catch up
3597545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if ((count % 5) == 0) {
3598545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      usleep(40000);
3599545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3600545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3601b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    i = (i + 1) & (gAllocRecordMax-1);
3602545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3603545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
3604545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3605545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesclass StringTable {
3606545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes public:
3607545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  StringTable() {
3608545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3609545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
36106d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  void Add(const char* s) {
3611545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    table_.insert(s);
3612545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3613545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3614a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t IndexOf(const char* s) const {
361502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    auto it = table_.find(s);
3616a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    if (it == table_.end()) {
3617a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes      LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
3618a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    }
3619a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    return std::distance(table_.begin(), it);
3620545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3621545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3622a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t Size() const {
3623545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return table_.size();
3624545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3625545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3626a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  void WriteTo(std::vector<uint8_t>& bytes) const {
362702e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const std::string& str : table_) {
362802e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      const char* s = str.c_str();
36296d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      size_t s_len = CountModifiedUtf8Chars(s);
36306d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      UniquePtr<uint16_t> s_utf16(new uint16_t[s_len]);
36316d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      ConvertModifiedUtf8ToUtf16(s_utf16.get(), s);
36326d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len);
3633545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3634545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3635545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3636545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes private:
3637a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  std::set<std::string> table_;
3638545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(StringTable);
3639545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
3640545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3641545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes/*
3642545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The data we send to DDMS contains everything we have recorded.
3643545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
3644545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * Message header (all values big-endian):
3645545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) message header len (to allow future expansion); includes itself
3646545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) entry header len
3647545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) stack frame len
3648545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of entries
3649545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (4b) offset to string table from start of message
3650545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of class name strings
3651545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of method name strings
3652545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of source file name strings
3653545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * For each entry:
3654545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (4b) total allocation size
3655221229cb523f849f165fdafbf9785010963715daElliott Hughes *   (2b) thread id
3656545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (2b) allocated object's class name index
3657545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (1b) stack depth
3658545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   For each stack frame:
3659545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method's class name
3660545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method name
3661545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method source file
3662545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) line number, clipped to 32767; -2 if native; -1 if no source
3663545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) class name strings
3664545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) method name strings
3665545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) source file strings
3666545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
3667545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * As with other DDM traffic, strings are sent as a 4-byte length
3668545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * followed by UTF-16 data.
3669545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
3670545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We send up 16-bit unsigned indexes into string tables.  In theory there
3671b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes * can be (kMaxAllocRecordStackDepth * gAllocRecordMax) unique strings in
3672545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * each table, but in practice there should be far fewer.
3673545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
3674545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The chief reason for using a string table here is to keep the size of
3675545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * the DDMS message to a minimum.  This is partly to make the protocol
3676545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * efficient, but also because we have to form the whole thing up all at
3677545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * once in a memory buffer.
3678545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
3679545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We use separate string tables for class names, method names, and source
3680545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * files to keep the indexes small.  There will generally be no overlap
3681545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * between the contents of these tables.
3682545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes */
3683545a064aca775ba801790fced3d713d8a87bfc61Elliott HughesjbyteArray Dbg::GetRecentAllocations() {
3684545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (false) {
3685545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    DumpRecentAllocations();
3686545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3687545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
368850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* self = Thread::Current();
368946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  std::vector<uint8_t> bytes;
369046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  {
369146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    MutexLock mu(self, gAllocTrackerLock);
369246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
369346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 1: generate string tables.
369446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
369546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable class_names;
369646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable method_names;
369746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable filenames;
369846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
369946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    int count = gAllocRecordCount;
370046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    int idx = HeadIndex();
370146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
370246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
370346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
370446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      class_names.Add(ClassHelper(record->type).GetDescriptor());
370546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
370646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      MethodHelper mh;
370746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) {
3708ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        mirror::ArtMethod* m = record->stack[i].method;
370946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        if (m != NULL) {
371046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          mh.ChangeMethod(m);
371146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          class_names.Add(mh.GetDeclaringClassDescriptor());
371246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          method_names.Add(mh.GetName());
371346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          filenames.Add(mh.GetDeclaringClassSourceFile());
371446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        }
371546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      }
3716545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3717b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      idx = (idx + 1) & (gAllocRecordMax-1);
371846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    }
371946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
372046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    LOG(INFO) << "allocation records: " << gAllocRecordCount;
372146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
372246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
372346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 2: Generate the output and store it in the buffer.
372446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
372546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
372646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) message header len (to allow future expansion); includes itself
372746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) entry header len
372846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) stack frame len
372946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kMessageHeaderLen = 15;
373046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kEntryHeaderLen = 9;
373146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kStackFrameLen = 8;
373246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kMessageHeaderLen);
373346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kEntryHeaderLen);
373446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kStackFrameLen);
373546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
373646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of entries
373746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (4b) offset to string table from start of message
373846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of class name strings
373946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of method name strings
374046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of source file name strings
374146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, gAllocRecordCount);
374246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    size_t string_table_offset = bytes.size();
37437934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Append4BE(bytes, 0);  // We'll patch this later...
374446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, class_names.Size());
374546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, method_names.Size());
374646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, filenames.Size());
374746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
374846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    count = gAllocRecordCount;
374946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    idx = HeadIndex();
375046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    ClassHelper kh;
375146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
375246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // For each entry:
375346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (4b) total allocation size
375446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) thread id
375546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) allocated object's class name index
375646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (1b) stack depth
375746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
375846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      size_t stack_depth = record->GetDepth();
375946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      kh.ChangeClass(record->type);
376046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      size_t allocated_object_class_name_index = class_names.IndexOf(kh.GetDescriptor());
376146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append4BE(bytes, record->byte_count);
376246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, record->thin_lock_id);
376346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, allocated_object_class_name_index);
376446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append1BE(bytes, stack_depth);
376546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
376646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      MethodHelper mh;
376746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
376846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // For each stack frame:
376946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method's class name
377046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method name
377146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method source file
377246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) line number, clipped to 32767; -2 if native; -1 if no source
377346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        mh.ChangeMethod(record->stack[stack_frame].method);
377446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        size_t class_name_index = class_names.IndexOf(mh.GetDeclaringClassDescriptor());
377546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        size_t method_name_index = method_names.IndexOf(mh.GetName());
377646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        size_t file_name_index = filenames.IndexOf(mh.GetDeclaringClassSourceFile());
377746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, class_name_index);
377846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, method_name_index);
377946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, file_name_index);
378046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber());
3781545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
378246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
3783b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      idx = (idx + 1) & (gAllocRecordMax-1);
3784545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3785545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
378646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) class name strings
378746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) method name strings
378846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) source file strings
378946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Set4BE(&bytes[string_table_offset], bytes.size());
379046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    class_names.WriteTo(bytes);
379146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    method_names.WriteTo(bytes);
379246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    filenames.WriteTo(bytes);
3793545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
379450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  JNIEnv* env = self->GetJniEnv();
3795545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  jbyteArray result = env->NewByteArray(bytes.size());
3796545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (result != NULL) {
3797545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
3798545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3799545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  return result;
3800545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
3801545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3802872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}  // namespace art
3803