debugger.cc revision 87118ed6f3f99e7df33214c277cf200a7b9a7499
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
9862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersclass DebugInstrumentationListener : public instrumentation::InstrumentationListener {
9962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers public:
10062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  DebugInstrumentationListener() {}
10162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual ~DebugInstrumentationListener() {}
10262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
10362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual void MethodEntered(Thread* thread, mirror::Object* this_object,
104ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                             const mirror::ArtMethod* method, uint32_t dex_pc)
10562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
10662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
10762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
10862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
10962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
110579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    Dbg::PostLocationEvent(method, 0, this_object, Dbg::kMethodEntry, nullptr);
11162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
11262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
11362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual void MethodExited(Thread* thread, mirror::Object* this_object,
114ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                            const mirror::ArtMethod* method,
11562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                            uint32_t dex_pc, const JValue& return_value)
11662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
11762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (method->IsNative()) {
11862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      // TODO: post location events is a suspension point and native method entry stubs aren't.
11962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return;
12062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    }
121579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    Dbg::PostLocationEvent(method, dex_pc, this_object, Dbg::kMethodExit, &return_value);
12262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
12362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
12451db44a194bafc3810a41164a8b39614f10e79dfSebastien Hertz  virtual void MethodUnwind(Thread* thread, mirror::Object* this_object,
12551db44a194bafc3810a41164a8b39614f10e79dfSebastien Hertz                            const mirror::ArtMethod* method, uint32_t dex_pc)
12651db44a194bafc3810a41164a8b39614f10e79dfSebastien Hertz      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
12762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    // We're not recorded to listen to this kind of event, so complain.
12862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    LOG(ERROR) << "Unexpected method unwind event in debugger " << PrettyMethod(method)
12951db44a194bafc3810a41164a8b39614f10e79dfSebastien Hertz               << " " << dex_pc;
13062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
13162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
13262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual void DexPcMoved(Thread* thread, mirror::Object* this_object,
133ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                          const mirror::ArtMethod* method, uint32_t new_dex_pc)
13462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
13562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    Dbg::UpdateDebugger(thread, this_object, method, new_dex_pc);
13662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
13762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
13862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  virtual void ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
139ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                               mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
14062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                               mirror::Throwable* exception_object)
14162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
14262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    Dbg::PostException(thread, throw_location, catch_method, catch_dex_pc, exception_object);
14362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
14462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers} gDebugInstrumentationListener;
14562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
1464ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes// JDWP is allowed unless the Zygote forbids it.
1474ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesstatic bool gJdwpAllowed = true;
1484ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
149c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Was there a -Xrunjdwp or -agentlib:jdwp= argument on the command line?
1503bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gJdwpConfigured = false;
1513bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
152c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes// Broken-down JDWP options. (Only valid if IsJdwpConfigured() is true.)
153376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughesstatic JDWP::JdwpOptions gJdwpOptions;
1543bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
1553bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes// Runtime JDWP state.
1563bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic JDWP::JdwpState* gJdwpState = NULL;
1573bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerConnected;  // debugger or DDMS is connected.
1583bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool gDebuggerActive;     // debugger is making requests.
1598696433d1b3d8ba15288483b777edd888de69135Elliott Hughesstatic bool gDisposed;           // debugger called VirtualMachine.Dispose, so we should drop the connection.
1603bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
16147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesstatic bool gDdmThreadNotification = false;
16247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
163767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes// DDMS GC-related settings.
164767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
165767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
166767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmHpsgWhat;
167767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
168767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesstatic Dbg::HpsgWhat gDdmNhsgWhat;
169767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
170475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesstatic ObjectRegistry* gRegistry = NULL;
171475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
172545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes// Recent allocation tracking.
173df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstromstatic Mutex gAllocTrackerLock DEFAULT_MUTEX_ACQUIRED_AFTER("AllocTracker lock");
1747934ac288acfb2552bb0b06ec1f61e5820d924a4Brian CarlstromAllocRecord* Dbg::recent_allocation_records_ PT_GUARDED_BY(gAllocTrackerLock) = NULL;  // TODO: CircularBuffer<AllocRecord>
175b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughesstatic size_t gAllocRecordMax GUARDED_BY(gAllocTrackerLock) = 0;
176f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughesstatic size_t gAllocRecordHead GUARDED_BY(gAllocTrackerLock) = 0;
177f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughesstatic size_t gAllocRecordCount GUARDED_BY(gAllocTrackerLock) = 0;
178545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
1798696433d1b3d8ba15288483b777edd888de69135Elliott Hughes// Breakpoints and single-stepping.
18009bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhaostatic std::vector<Breakpoint> gBreakpoints GUARDED_BY(Locks::breakpoint_lock_);
1818696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
182ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic bool IsBreakpoint(const mirror::ArtMethod* m, uint32_t dex_pc)
18309bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    LOCKS_EXCLUDED(Locks::breakpoint_lock_)
184b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
18509bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
1868696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  for (size_t i = 0; i < gBreakpoints.size(); ++i) {
187a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes    if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == dex_pc) {
1888696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      VLOG(jdwp) << "Hit breakpoint #" << i << ": " << gBreakpoints[i];
1898696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      return true;
1908696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
1918696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
1928696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return false;
1938696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
1948696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
1959e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughesstatic bool IsSuspendedForDebugger(ScopedObjectAccessUnchecked& soa, Thread* thread) {
1969e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_suspend_count_lock_);
1979e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // A thread may be suspended for GC; in this code, we really want to know whether
1989e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  // there's a debugger suspension active.
1999e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  return thread->IsSuspended() && thread->GetDebugSuspendCount() > 0;
2009e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes}
2019e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
2022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Array* DecodeArray(JDWP::RefTypeId id, JDWP::JdwpError& status)
203b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2042dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
20564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
206436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_OBJECT;
207436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
208436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
209436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (!o->IsArrayInstance()) {
210436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_ARRAY;
211436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
212436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
213436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
214436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsArray();
215436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
216436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
2172dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic mirror::Class* DecodeClass(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->IsClass()) {
225436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    status = JDWP::ERR_INVALID_CLASS;
226436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return NULL;
227436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
228436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  status = JDWP::ERR_NONE;
229436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return o->AsClass();
230436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
231436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
232221229cb523f849f165fdafbf9785010963715daElliott Hughesstatic JDWP::JdwpError DecodeThread(ScopedObjectAccessUnchecked& soa, JDWP::ObjectId thread_id, Thread*& thread)
233a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    EXCLUSIVE_LOCKS_REQUIRED(Locks::thread_list_lock_)
234b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
235b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
2362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_peer = gRegistry->Get<mirror::Object*>(thread_id);
23764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_peer == NULL || thread_peer == ObjectRegistry::kInvalidObject) {
238221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't even an object.
239221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_OBJECT;
240221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
241221229cb523f849f165fdafbf9785010963715daElliott Hughes
2422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* java_lang_Thread = soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_Thread);
243221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (!java_lang_Thread->IsAssignableFrom(thread_peer->GetClass())) {
244221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This isn't a thread.
245221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_INVALID_THREAD;
246221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
247221229cb523f849f165fdafbf9785010963715daElliott Hughes
248221229cb523f849f165fdafbf9785010963715daElliott Hughes  thread = Thread::FromManagedThread(soa, thread_peer);
249221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (thread == NULL) {
250221229cb523f849f165fdafbf9785010963715daElliott Hughes    // This is a java.lang.Thread without a Thread*. Must be a zombie.
251221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_THREAD_NOT_ALIVE;
252436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
253221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
254436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes}
255436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
25624437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic JDWP::JdwpTag BasicTagFromDescriptor(const char* descriptor) {
25724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // JDWP deliberately uses the descriptor characters' ASCII values for its enum.
25824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  // Note that by "basic" we mean that we don't get more specific than JT_OBJECT.
25924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  return static_cast<JDWP::JdwpTag>(descriptor[0]);
26024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
26124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
2622dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic JDWP::JdwpTag TagFromClass(mirror::Class* c)
263b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
26486b0010c79ef95b5333cd540b7d3af34a9f1a643Elliott Hughes  CHECK(c != NULL);
26524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsArrayClass()) {
26624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_ARRAY;
26724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
26824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
2693d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
27024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (c->IsStringClass()) {
27124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_STRING;
27224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  } else if (c->IsClassClass()) {
27324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_CLASS_OBJECT;
2743d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else if (class_linker->FindSystemClass("Ljava/lang/Thread;")->IsAssignableFrom(c)) {
27524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_THREAD;
2763d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else if (class_linker->FindSystemClass("Ljava/lang/ThreadGroup;")->IsAssignableFrom(c)) {
27724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_THREAD_GROUP;
2783d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else if (class_linker->FindSystemClass("Ljava/lang/ClassLoader;")->IsAssignableFrom(c)) {
27924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_CLASS_LOADER;
28024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  } else {
28124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return JDWP::JT_OBJECT;
28224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
28324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
28424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
28524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes/*
28624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Objects declared to hold Object might actually hold a more specific
28724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * type.  The debugger may take a special interest in these (e.g. it
28824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * wants to display the contents of Strings), so we want to return an
28924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * appropriate tag.
29024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes *
29124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes * Null objects are tagged JT_OBJECT.
29224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes */
2932dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersstatic JDWP::JdwpTag TagFromObject(const mirror::Object* o)
294b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
29524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  return (o == NULL) ? JDWP::JT_OBJECT : TagFromClass(o->GetClass());
29624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
29724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
29824437995cdac88b42e42b16d9aa121e833330999Elliott Hughesstatic bool IsPrimitiveTag(JDWP::JdwpTag tag) {
29924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  switch (tag) {
30024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BOOLEAN:
30124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_BYTE:
30224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_CHAR:
30324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_FLOAT:
30424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_DOUBLE:
30524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_INT:
30624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_LONG:
30724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_SHORT:
30824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  case JDWP::JT_VOID:
30924437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return true;
31024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  default:
31124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    return false;
31224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
31324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes}
31424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
3153bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
3163bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Handle one of the JDWP name/value pairs.
3173bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
3183bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * JDWP options are:
3193bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  help: if specified, show help message and bail
3203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  transport: may be dt_socket or dt_shmem
3213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  address: for dt_socket, "host:port", or just "port" when listening
3223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  server: if "y", wait for debugger to attach; if "n", attach to debugger
3233bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  timeout: how long to wait for debugger to connect / listen
3243bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
3253bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Useful with server=n (these aren't supported yet):
3263bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onthrow=<exception-name>: connect to debugger when exception thrown
3273bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  onuncaught=y|n: connect to debugger when uncaught exception thrown
3283bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *  launch=<command-line>: launch the debugger itself
3293bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes *
3303bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * The "transport" option is required, as is "address" if server=n.
3313bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
3323bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesstatic bool ParseJdwpOption(const std::string& name, const std::string& value) {
3333bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (name == "transport") {
3343bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "dt_socket") {
335376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportSocket;
3363bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "dt_android_adb") {
337376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.transport = JDWP::kJdwpTransportAndroidAdb;
3383bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
3393bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP transport not supported: " << value;
3403bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
3413bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
3423bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "server") {
3433bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
344376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = false;
3453bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
346376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.server = true;
3473bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
3483bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'server' must be 'y' or 'n'";
3493bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
3503bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
3513bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "suspend") {
3523bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (value == "n") {
353376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = false;
3543bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else if (value == "y") {
355376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.suspend = true;
3563bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
3573bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP option 'suspend' must be 'y' or 'n'";
3583bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
3593bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
3603bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "address") {
3613bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* this is either <port> or <host>:<port> */
3623bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string port_string;
363376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.host.clear();
3643bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type colon = value.find(':');
3653bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (colon != std::string::npos) {
366376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes      gJdwpOptions.host = value.substr(0, colon);
3673bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value.substr(colon + 1);
3683bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    } else {
3693bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      port_string = value;
3703bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
3713bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (port_string.empty()) {
3723bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address missing port: " << value;
3733bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
3743bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
3753bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    char* end;
376ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    uint64_t port = strtoul(port_string.c_str(), &end, 10);
377ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    if (*end != '\0' || port > 0xffff) {
3783bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "JDWP address has junk in port field: " << value;
3793bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
3803bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
381376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    gJdwpOptions.port = port;
3823bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else if (name == "launch" || name == "onthrow" || name == "oncaught" || name == "timeout") {
3833bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    /* valid but unsupported */
3843bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring JDWP option '" << name << "'='" << value << "'";
3853bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
3863bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(INFO) << "Ignoring unrecognized JDWP option '" << name << "'='" << value << "'";
3873bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
3883bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
3893bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
3903bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
3913bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
3923bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes/*
3933bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * Parse the latter half of a -Xrunjdwp/-agentlib:jdwp= string, e.g.:
3943bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes * "transport=dt_socket,address=8000,server=y,suspend=n"
3953bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes */
3963bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughesbool Dbg::ParseJdwpOptions(const std::string& options) {
3974dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "ParseJdwpOptions: " << options;
39847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3993bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  std::vector<std::string> pairs;
4003bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Split(options, ',', pairs);
4013bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
4023bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  for (size_t i = 0; i < pairs.size(); ++i) {
4033bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    std::string::size_type equals = pairs[i].find('=');
4043bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    if (equals == std::string::npos) {
4053bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      LOG(ERROR) << "Can't parse JDWP option '" << pairs[i] << "' in '" << options << "'";
4063bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes      return false;
4073bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    }
4083bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    ParseJdwpOption(pairs[i].substr(0, equals), pairs[i].substr(equals + 1));
4093bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
4103bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
411376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpOptions.transport == JDWP::kJdwpTransportUnknown) {
4123bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP transport: " << options;
4133bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
414376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (!gJdwpOptions.server && (gJdwpOptions.host.empty() || gJdwpOptions.port == 0)) {
4153bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    LOG(ERROR) << "Must specify JDWP host and port when server=n: " << options;
4163bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes    return false;
4173bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
4183bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
4193bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gJdwpConfigured = true;
4203bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return true;
4213bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes}
4223bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes
423d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StartJdwp() {
424c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!gJdwpAllowed || !IsJdwpConfigured()) {
425376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    // No JDWP for you!
426376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    return;
427376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  }
428376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes
429475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  CHECK(gRegistry == NULL);
430475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gRegistry = new ObjectRegistry;
431475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
432d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // Init JDWP if the debugger is enabled. This may connect out to a
433d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger, passively listen for a debugger, or block waiting for a
434d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // debugger.
435376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  gJdwpState = JDWP::JdwpState::Create(&gJdwpOptions);
436376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState == NULL) {
437f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // We probably failed because some other process has the port already, which means that
438f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // if we don't abort the user is likely to think they're talking to us when they're actually
439f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes    // talking to that other process.
4403d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Debugger thread failed to initialize";
441d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
442d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes
443d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // If a debugger has already attached, send the "welcome" message.
444d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  // This may cause us to suspend all threads.
445376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  if (gJdwpState->IsActive()) {
44600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
447376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes    if (!gJdwpState->PostVMStart()) {
4483d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes      LOG(WARNING) << "Failed to post 'start' message to debugger";
449d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes    }
450d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughes  }
451872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
452872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
453d1cc8363d4f4bbac7568b1d02a5ca481cd10830fElliott Hughesvoid Dbg::StopJdwp() {
454376a7a033d29d5f2b6e16574a340c999ff2999a0Elliott Hughes  delete gJdwpState;
455475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  delete gRegistry;
456475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gRegistry = NULL;
457872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
458872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
459767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesvoid Dbg::GcDidFinish() {
460767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
46100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
46281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes    LOG(DEBUG) << "Sending heap info to DDM";
4637162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(gDdmHpifWhen);
464767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
465767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
46600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
46781ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes    LOG(DEBUG) << "Dumping heap to DDM";
4686a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(false);
469767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
470767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
47100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
472767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(DEBUG) << "Dumping native heap to DDM";
4736a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    DdmSendHeapSegments(true);
474767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
475767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
476767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
4774ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughesvoid Dbg::SetJdwpAllowed(bool allowed) {
4784ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes  gJdwpAllowed = allowed;
4794ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes}
4804ffd31315bc0d00ec278e85feed15985de5ac3dcElliott Hughes
481872d4ec7225444d9400d30f9027247deb91012fdElliott HughesDebugInvokeReq* Dbg::GetInvokeReq() {
482475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return Thread::Current()->GetInvokeReq();
483475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
484475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
485475fc23a4a7f35d1be87ea0b06c80df317a720acElliott HughesThread* Dbg::GetDebugThread() {
486475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  return (gJdwpState != NULL) ? gJdwpState->GetDebugThread() : NULL;
487475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes}
488475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes
489475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::ClearWaitForEventThread() {
490475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  gJdwpState->ClearWaitForEventThread();
491872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
492872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
493872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Connected() {
4943bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(!gDebuggerConnected);
4954dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "JDWP has attached";
4963bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  gDebuggerConnected = true;
4978696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = false;
4988696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
4998696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
5008696433d1b3d8ba15288483b777edd888de69135Elliott Hughesvoid Dbg::Disposed() {
5018696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  gDisposed = true;
5028696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
5038696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
5048696433d1b3d8ba15288483b777edd888de69135Elliott Hughesbool Dbg::IsDisposed() {
5058696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  return gDisposed;
506872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
507872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
508a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::GoActive() {
509a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Enable all debugging features, including scans for breakpoints.
510a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // This is a no-op if we're already active.
511a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Only called from the JDWP handler thread.
512a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (gDebuggerActive) {
513a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return;
514a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
515a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
516c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  {
517c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    // TODO: dalvik only warned if there were breakpoints left over. clear in Dbg::Disconnected?
51809bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao    MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
519c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes    CHECK_EQ(gBreakpoints.size(), 0U);
520c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  }
521a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
52262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
52362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
52462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
52562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
52662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_NE(old_state, kRunnable);
52762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetInstrumentation()->AddListener(&gDebugInstrumentationListener,
52862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                             instrumentation::Instrumentation::kMethodEntered |
52962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                             instrumentation::Instrumentation::kMethodExited |
53014dd5a82ad85a28612bf86e755ff929d767cea4cJeff Hao                                             instrumentation::Instrumentation::kDexPcMoved |
53114dd5a82ad85a28612bf86e755ff929d767cea4cJeff Hao                                             instrumentation::Instrumentation::kExceptionCaught);
532a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  gDebuggerActive = true;
53362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
53462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
53562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
53662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  LOG(INFO) << "Debugger is active";
537872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
538872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
539872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::Disconnected() {
540234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  CHECK(gDebuggerConnected);
541234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
542c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  LOG(INFO) << "Debugger is no longer active";
543234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes
54462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // Suspend all threads and exclusively acquire the mutator lock. Set the state of the thread
54562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // to kRunnable to avoid scoped object access transitions. Remove the debugger as a listener
54662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  // and clear the object registry.
54762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Runtime* runtime = Runtime::Current();
54862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->SuspendAll();
54962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  Thread* self = Thread::Current();
55062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  ThreadState old_state = self->SetStateUnsafe(kRunnable);
55162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetInstrumentation()->RemoveListener(&gDebugInstrumentationListener,
55262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                                instrumentation::Instrumentation::kMethodEntered |
55362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                                instrumentation::Instrumentation::kMethodExited |
55414dd5a82ad85a28612bf86e755ff929d767cea4cJeff Hao                                                instrumentation::Instrumentation::kDexPcMoved |
55514dd5a82ad85a28612bf86e755ff929d767cea4cJeff Hao                                                instrumentation::Instrumentation::kExceptionCaught);
556c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  gDebuggerActive = false;
557234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gRegistry->Clear();
558234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  gDebuggerConnected = false;
55962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  CHECK_EQ(self->SetStateUnsafe(old_state), kRunnable);
56062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  runtime->GetThreadList()->ResumeAll();
561872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
562872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
563c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsDebuggerActive() {
5643bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gDebuggerActive;
565872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
566872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
567c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughesbool Dbg::IsJdwpConfigured() {
5683bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  return gJdwpConfigured;
569872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
570872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
571872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesint64_t Dbg::LastDebuggerActivity() {
572ca9515205010099d006ac2fac244348a1e673dcbElliott Hughes  return gJdwpState->LastDebuggerActivity();
573872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
574872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
575872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::UndoDebuggerSuspensions() {
576234ab15b00f8120282d1833e5d7480eca2e35a29Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
577872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
578872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
57988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetClassName(JDWP::RefTypeId class_id) {
5802dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(class_id);
5813d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (o == NULL) {
582436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return "NULL";
5837b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
58464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
58588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    return StringPrintf("invalid object %p", reinterpret_cast<void*>(class_id));
5863d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
5873d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (!o->IsClass()) {
5887934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    return StringPrintf("non-class %p", o);  // This is only used for debugging output anyway.
5893d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
590436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return DescriptorToName(ClassHelper(o->AsClass()).GetDescriptor());
5913d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes}
5923d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
59388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassObject(JDWP::RefTypeId id, JDWP::ObjectId& class_object_id) {
594436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
5952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
596436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
597436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
5982435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
59988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  class_object_id = gRegistry->Add(c);
600436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
6018696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
6028696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
60388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSuperclass(JDWP::RefTypeId id, JDWP::RefTypeId& superclass_id) {
6043d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
6052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
6063d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c == NULL) {
6073d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
6083d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
6093d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (c->IsInterface()) {
6103d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    // http://code.google.com/p/android/issues/detail?id=20856
61188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = 0;
6123d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  } else {
61388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes    superclass_id = gRegistry->Add(c->GetSuperClass());
6143d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
6153d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
616872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
617872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
618436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetClassLoader(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
6192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
62064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
621436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
622436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  }
623436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAddObjectId(pReply, gRegistry->Add(o->GetClass()->GetClassLoader()));
624436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
625872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
626872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
627436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott HughesJDWP::JdwpError Dbg::GetModifiers(JDWP::RefTypeId id, JDWP::ExpandBuf* pReply) {
628436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
6292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(id, status);
630436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
631436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
6327b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
633436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
634436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  uint32_t access_flags = c->GetAccessFlags() & kAccJavaFlagsMask;
635436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
636436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  // Set ACC_SUPER; dex files don't contain this flag, but all classes are supposed to have it set.
637436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  // Class.getModifiers doesn't return it, but JDWP does, so we set it here.
638436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  access_flags |= kAccSuper;
639436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
640436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAdd4BE(pReply, access_flags);
641436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
642436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
643872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
644872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
645f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott HughesJDWP::JdwpError Dbg::GetMonitorInfo(JDWP::ObjectId object_id, JDWP::ExpandBuf* reply)
646f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
6472dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
64864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
649f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
650f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
651f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
652f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  // Ensure all threads are suspended while we read objects' lock words.
653f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Thread* self = Thread::Current();
654f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Locks::mutator_lock_->SharedUnlock(self);
655f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Locks::mutator_lock_->ExclusiveLock(self);
656f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
657f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  MonitorInfo monitor_info(o);
658f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
659f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Locks::mutator_lock_->ExclusiveUnlock(self);
660f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  Locks::mutator_lock_->SharedLock(self);
661f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
662d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  if (monitor_info.owner_ != NULL) {
663d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.owner_->GetPeer()));
664f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else {
665f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes    expandBufAddObjectId(reply, gRegistry->Add(NULL));
666f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
667d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.entry_count_);
668d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  expandBufAdd4BE(reply, monitor_info.waiters_.size());
669d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  for (size_t i = 0; i < monitor_info.waiters_.size(); ++i) {
670d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    expandBufAddObjectId(reply, gRegistry->Add(monitor_info.waiters_[i]->GetPeer()));
671f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  }
672f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  return JDWP::ERR_NONE;
673f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes}
674f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes
675734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott HughesJDWP::JdwpError Dbg::GetOwnedMonitors(JDWP::ObjectId thread_id,
676734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes                                      std::vector<JDWP::ObjectId>& monitors,
677734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes                                      std::vector<uint32_t>& stack_depths)
6784993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
6794993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
6804993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
6814993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  Thread* thread;
6824993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
6834993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  if (error != JDWP::ERR_NONE) {
6844993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return error;
6854993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
6864993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
6874993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
6884993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
6894993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
6904993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  struct OwnedMonitorVisitor : public StackVisitor {
6917a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    OwnedMonitorVisitor(Thread* thread, Context* context)
6924993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
6937a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context), current_stack_depth(0) {}
6944993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
6954993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
6964993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    // annotalysis.
6974993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
6984993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      if (!GetMethod()->IsRuntimeMethod()) {
6994993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes        Monitor::VisitLocks(this, AppendOwnedMonitors, this);
700734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes        ++current_stack_depth;
7014993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      }
7024993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes      return true;
7034993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
7044993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
7052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static void AppendOwnedMonitors(mirror::Object* owned_monitor, void* arg) {
7067a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      OwnedMonitorVisitor* visitor = reinterpret_cast<OwnedMonitorVisitor*>(arg);
707734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes      visitor->monitors.push_back(owned_monitor);
708734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes      visitor->stack_depths.push_back(visitor->current_stack_depth);
7094993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    }
7104993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
711734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    size_t current_stack_depth;
7122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    std::vector<mirror::Object*> monitors;
713734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    std::vector<uint32_t> stack_depths;
7144993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  };
7157a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  UniquePtr<Context> context(Context::Create());
7167a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  OwnedMonitorVisitor visitor(thread, context.get());
7174993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  visitor.WalkStack();
7184993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
7194993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  for (size_t i = 0; i < visitor.monitors.size(); ++i) {
7204993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes    monitors.push_back(gRegistry->Add(visitor.monitors[i]));
721734b8c62cc637c25f4c2481ca6a48adbd7209c6cElliott Hughes    stack_depths.push_back(visitor.stack_depths[i]);
7224993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  }
7234993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
7244993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes  return JDWP::ERR_NONE;
7254993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes}
7264993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes
727f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott HughesJDWP::JdwpError Dbg::GetContendedMonitor(JDWP::ObjectId thread_id, JDWP::ObjectId& contended_monitor)
728f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
729f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccessUnchecked soa(Thread::Current());
730f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
731f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
732f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
733f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
734f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
735f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
736f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
737f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
738f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
739f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
740f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  contended_monitor = gRegistry->Add(Monitor::GetContendedMonitor(thread));
741f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
742f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
743f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
744f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
745ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott HughesJDWP::JdwpError Dbg::GetInstanceCounts(const std::vector<JDWP::RefTypeId>& class_ids,
746ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes                                       std::vector<uint64_t>& counts)
747ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
7482dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
749ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  counts.clear();
750ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  for (size_t i = 0; i < class_ids.size(); ++i) {
751ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    JDWP::JdwpError status;
7522dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_ids[i], status);
753ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    if (c == NULL) {
754ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes      return status;
755ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    }
756ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    classes.push_back(c);
757ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes    counts.push_back(0);
758ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  }
759ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes
760ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  Runtime::Current()->GetHeap()->CountInstances(classes, false, &counts[0]);
761ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes  return JDWP::ERR_NONE;
762ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes}
763ec0f83d95e2174c97e93279ffa71642be7e12b60Elliott Hughes
7643b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott HughesJDWP::JdwpError Dbg::GetInstances(JDWP::RefTypeId class_id, int32_t max_count, std::vector<JDWP::ObjectId>& instances)
7653b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
7663b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  JDWP::JdwpError status;
7672dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
7683b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  if (c == NULL) {
7693b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    return status;
7703b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
7713b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes
7722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
7733b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  Runtime::Current()->GetHeap()->GetInstances(c, max_count, raw_instances);
7743b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
7753b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes    instances.push_back(gRegistry->Add(raw_instances[i]));
7763b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  }
7773b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes  return JDWP::ERR_NONE;
7783b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes}
7793b78c949ab839d21454bc6f18c7640d2ae8c22f3Elliott Hughes
7800cbaff584244ee767027aff35cd3c625aaee2994Elliott HughesJDWP::JdwpError Dbg::GetReferringObjects(JDWP::ObjectId object_id, int32_t max_count,
7810cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes                                         std::vector<JDWP::ObjectId>& referring_objects)
7820cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
7832dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
78464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
7850cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
7860cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
7870cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes
7882dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Object*> raw_instances;
7890cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  Runtime::Current()->GetHeap()->GetReferringObjects(o, max_count, raw_instances);
7900cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  for (size_t i = 0; i < raw_instances.size(); ++i) {
7910cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes    referring_objects.push_back(gRegistry->Add(raw_instances[i]));
7920cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  }
7930cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes  return JDWP::ERR_NONE;
7940cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes}
7950cbaff584244ee767027aff35cd3c625aaee2994Elliott Hughes
79664f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::DisableCollection(JDWP::ObjectId object_id)
79764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
79864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisableCollection(object_id);
79964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
80064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
80164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
80264f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::EnableCollection(JDWP::ObjectId object_id)
80364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
80464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->EnableCollection(object_id);
80564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
80664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
80764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
80864f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::IsCollected(JDWP::ObjectId object_id, bool& is_collected)
80964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
81064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  is_collected = gRegistry->IsCollected(object_id);
81164f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  return JDWP::ERR_NONE;
81264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
81364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
81464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughesvoid Dbg::DisposeObject(JDWP::ObjectId object_id, uint32_t reference_count)
81564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
81664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  gRegistry->DisposeObject(object_id, reference_count);
81764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes}
81864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes
81988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetReflectedType(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
820436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
8212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
822436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
823436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
8247b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
825436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
826436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  expandBufAdd1(pReply, c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS);
82788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  expandBufAddRefTypeId(pReply, class_id);
828436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
829872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
830872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
8317b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughesvoid Dbg::GetClassList(std::vector<JDWP::RefTypeId>& classes) {
832a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Get the complete list of reference classes (i.e. all classes except
833a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // the primitive types).
834a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  // Returns a newly-allocated buffer full of RefTypeId values.
835a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  struct ClassListCreator {
836ba8eee10607a524f43b55a6f33c13924fb16d435Elliott Hughes    explicit ClassListCreator(std::vector<JDWP::RefTypeId>& classes) : classes(classes) {
8377b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    }
8387b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
8392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    static bool Visit(mirror::Class* c, void* arg) {
840a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return reinterpret_cast<ClassListCreator*>(arg)->Visit(c);
841a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
842a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
84364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
84464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
84564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool Visit(mirror::Class* c) NO_THREAD_SAFETY_ANALYSIS {
846a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (!c->IsPrimitive()) {
84764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes        classes.push_back(gRegistry->AddRefType(c));
848a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
849a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return true;
850a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
851a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
8527b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes    std::vector<JDWP::RefTypeId>& classes;
853a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
854a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
8557b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  ClassListCreator clc(classes);
856a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Runtime::Current()->GetClassLinker()->VisitClasses(ClassListCreator::Visit, &clc);
857872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
858872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
85988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetClassInfo(JDWP::RefTypeId class_id, JDWP::JdwpTypeTag* pTypeTag, uint32_t* pStatus, std::string* pDescriptor) {
860436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
8612dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
862436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
863436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
8647b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
8657b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes
866a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (c->IsArrayClass()) {
867a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
868a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = JDWP::TT_ARRAY;
869a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
870a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsErroneous()) {
871a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_ERROR;
872a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    } else {
873a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      *pStatus = JDWP::CS_VERIFIED | JDWP::CS_PREPARED | JDWP::CS_INITIALIZED;
874a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
875a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    *pTypeTag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
876a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
877a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
878a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (pDescriptor != NULL) {
879dfb325e0ddd746cd8f7c2e3723b3a573eb7cc111Ian Rogers    *pDescriptor = ClassHelper(c).GetDescriptor();
880a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
881436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
882872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
883872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
884c3b77c7c2971124cbf3b2d9da64e7a8a9a649f2eElliott Hughesvoid Dbg::FindLoadedClassBySignature(const char* descriptor, std::vector<JDWP::RefTypeId>& ids) {
8852dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  std::vector<mirror::Class*> classes;
8866fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  Runtime::Current()->GetClassLinker()->LookupClasses(descriptor, classes);
8876fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  ids.clear();
8886fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  for (size_t i = 0; i < classes.size(); ++i) {
8896fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes    ids.push_back(gRegistry->Add(classes[i]));
8906fa602d614d418f38afebb4d44f42e7dc0b4bd94Elliott Hughes  }
891872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
892872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
89364f574f474aa77c72778640ab21f8cfa72546812Elliott HughesJDWP::JdwpError Dbg::GetReferenceType(JDWP::ObjectId object_id, JDWP::ExpandBuf* pReply)
89464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
8952dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
89664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == NULL || o == ObjectRegistry::kInvalidObject) {
8972435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
8982435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
8992435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
9002435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  JDWP::JdwpTypeTag type_tag;
901499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  if (o->GetClass()->IsArrayClass()) {
9022435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    type_tag = JDWP::TT_ARRAY;
903499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  } else if (o->GetClass()->IsInterface()) {
9042435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    type_tag = JDWP::TT_INTERFACE;
905499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  } else {
9062435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    type_tag = JDWP::TT_CLASS;
907499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  }
90864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId type_id = gRegistry->AddRefType(o->GetClass());
9092435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
9102435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAdd1(pReply, type_tag);
9112435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAddRefTypeId(pReply, type_id);
9122435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
9132435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
914872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
915872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
916fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan RogersJDWP::JdwpError Dbg::GetSignature(JDWP::RefTypeId class_id, std::string* signature) {
9171fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  JDWP::JdwpError status;
9182dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
9191fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  if (c == NULL) {
9201fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes    return status;
9217b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
922dfb325e0ddd746cd8f7c2e3723b3a573eb7cc111Ian Rogers  *signature = ClassHelper(c).GetDescriptor();
9231fe7afb88498d48d1f808ee31e986324f2a1c842Elliott Hughes  return JDWP::ERR_NONE;
924872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
925872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
92688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetSourceFile(JDWP::RefTypeId class_id, std::string& result) {
927436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
9282dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
929436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
930436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
9317b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
932436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  result = ClassHelper(c).GetSourceFile();
933436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
934872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
935872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
93688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetObjectTag(JDWP::ObjectId object_id, uint8_t& tag) {
9372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
93864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (o == ObjectRegistry::kInvalidObject) {
939546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
940546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
941546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  tag = TagFromObject(o);
942546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  return JDWP::ERR_NONE;
943872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
944872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
945aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughessize_t Dbg::GetTagWidth(JDWP::JdwpTag tag) {
946dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  switch (tag) {
947dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_VOID:
948dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 0;
949dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BYTE:
950dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_BOOLEAN:
951dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 1;
952dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CHAR:
953dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_SHORT:
954dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 2;
955dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_FLOAT:
956dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_INT:
957dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 4;
958dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_ARRAY:
959dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_OBJECT:
960dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_STRING:
961dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD:
962dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_THREAD_GROUP:
963dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_LOADER:
964dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_CLASS_OBJECT:
965dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return sizeof(JDWP::ObjectId);
966dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_DOUBLE:
967dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  case JDWP::JT_LONG:
968dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return 8;
969dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  default:
9703d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes    LOG(FATAL) << "Unknown tag " << tag;
971dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    return -1;
972dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
973872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
974872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
97588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetArrayLength(JDWP::ObjectId array_id, int& length) {
9763d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
9772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
9783d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (a == NULL) {
9793d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
98024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
9813d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  length = a->GetLength();
9823d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
983872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
984872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
98588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputArray(JDWP::ObjectId array_id, int offset, int count, JDWP::ExpandBuf* pReply) {
9863d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
9872dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Array* a = DecodeArray(array_id, status);
9883d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  if (a == NULL) {
9893d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
9903d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
99124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
99224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (offset < 0 || count < 0 || offset > a->GetLength() || a->GetLength() - offset < count) {
99324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
9943d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
99524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
9966d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  std::string descriptor(ClassHelper(a->GetClass()).GetDescriptor());
99724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor.c_str() + 1);
99824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
9993d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd1(pReply, tag);
10003d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  expandBufAdd4BE(pReply, count);
10013d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
100224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  if (IsPrimitiveTag(tag)) {
100324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    size_t width = GetTagWidth(tag);
100424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    uint8_t* dst = expandBufAddSpace(pReply, count * width);
100524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    if (width == 8) {
1006a15e67d5ee5aa9615596cee2be42c2b2caf128c6Ian Rogers      const uint64_t* src8 = reinterpret_cast<uint64_t*>(a->GetRawData(sizeof(uint64_t)));
100724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write8BE(&dst, src8[offset + i]);
100824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 4) {
1009a15e67d5ee5aa9615596cee2be42c2b2caf128c6Ian Rogers      const uint32_t* src4 = reinterpret_cast<uint32_t*>(a->GetRawData(sizeof(uint32_t)));
101024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write4BE(&dst, src4[offset + i]);
101124437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else if (width == 2) {
1012a15e67d5ee5aa9615596cee2be42c2b2caf128c6Ian Rogers      const uint16_t* src2 = reinterpret_cast<uint16_t*>(a->GetRawData(sizeof(uint16_t)));
101324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      for (int i = 0; i < count; ++i) JDWP::Write2BE(&dst, src2[offset + i]);
101424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    } else {
1015a15e67d5ee5aa9615596cee2be42c2b2caf128c6Ian Rogers      const uint8_t* src = reinterpret_cast<uint8_t*>(a->GetRawData(sizeof(uint8_t)));
101624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      memcpy(dst, &src[offset * width], count * width);
101724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
101824437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  } else {
10192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::ObjectArray<mirror::Object>* oa = a->AsObjectArray<mirror::Object>();
102024437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    for (int i = 0; i < count; ++i) {
10212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* element = oa->Get(offset + i);
102224437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      JDWP::JdwpTag specific_tag = (element != NULL) ? TagFromObject(element) : tag;
102324437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAdd1(pReply, specific_tag);
102424437995cdac88b42e42b16d9aa121e833330999Elliott Hughes      expandBufAddObjectId(pReply, gRegistry->Add(element));
102524437995cdac88b42e42b16d9aa121e833330999Elliott Hughes    }
102624437995cdac88b42e42b16d9aa121e833330999Elliott Hughes  }
102724437995cdac88b42e42b16d9aa121e833330999Elliott Hughes
10283d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1029872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1030872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
10314b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughestemplate <typename T> void CopyArrayData(mirror::Array* a, JDWP::Request& src, int offset, int count) {
10324b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  DCHECK(a->GetClass()->IsPrimitiveArray());
10334b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
10344b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  T* dst = &(reinterpret_cast<T*>(a->GetRawData(sizeof(T)))[offset * sizeof(T)]);
10354b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  for (int i = 0; i < count; ++i) {
10364b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    *dst++ = src.ReadValue(sizeof(T));
10374b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  }
10384b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes}
10394b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
104088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetArrayElements(JDWP::ObjectId array_id, int offset, int count,
10414b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                      JDWP::Request& request)
1042b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
10433d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  JDWP::JdwpError status;
10444b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  mirror::Array* dst = DecodeArray(array_id, status);
10454b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (dst == NULL) {
10463d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return status;
10473d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  }
1048f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
10494b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (offset < 0 || count < 0 || offset > dst->GetLength() || dst->GetLength() - offset < count) {
1050f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    LOG(WARNING) << __FUNCTION__ << " access out of bounds: offset=" << offset << "; count=" << count;
10513d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    return JDWP::ERR_INVALID_LENGTH;
1052f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1053fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan Rogers  const char* descriptor = ClassHelper(dst->GetClass()).GetDescriptor();
1054fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(descriptor + 1);
1055f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
1056f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  if (IsPrimitiveTag(tag)) {
1057f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    size_t width = GetTagWidth(tag);
1058f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    if (width == 8) {
10594b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint64_t>(dst, request, offset, count);
1060f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 4) {
10614b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint32_t>(dst, request, offset, count);
1062f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else if (width == 2) {
10634b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint16_t>(dst, request, offset, count);
1064f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    } else {
10654b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      CopyArrayData<uint8_t>(dst, request, offset, count);
1066f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1067f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  } else {
10684b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    mirror::ObjectArray<mirror::Object>* oa = dst->AsObjectArray<mirror::Object>();
1069f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    for (int i = 0; i < count; ++i) {
10704b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes      JDWP::ObjectId id = request.ReadObjectId();
10712dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* o = gRegistry->Get<mirror::Object*>(id);
107264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes      if (o == ObjectRegistry::kInvalidObject) {
1073436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
1074436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes      }
1075436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes      oa->Set(offset + i, o);
1076f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes    }
1077f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes  }
1078f03b8f66511a2d8cb78d306f36feb51392232a5eElliott Hughes
10793d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1080872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1081872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
10827b3cdfcca472b779cf8745fb8460935e56229f11Elliott HughesJDWP::ObjectId Dbg::CreateString(const std::string& str) {
10832dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  return gRegistry->Add(mirror::String::AllocFromModifiedUtf8(Thread::Current(), str.c_str()));
1084872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1085872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
108688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateObject(JDWP::RefTypeId class_id, JDWP::ObjectId& new_object) {
1087436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
10882dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1089436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1090436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
10917b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
109250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  new_object = gRegistry->Add(c->AllocObject(Thread::Current()));
1093436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1094872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1095872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1096bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes/*
1097bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes * Used by Eclipse's "Display" view to evaluate "new byte[5]" to get "(byte[]) [0, 0, 0, 0, 0]".
1098bf13d36a25ec0a217fc4440c38b03d10f6c7f642Elliott Hughes */
109988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::CreateArrayObject(JDWP::RefTypeId array_class_id, uint32_t length,
110000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                       JDWP::ObjectId& new_array) {
1101436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(array_class_id, status);
1103436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1104436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
11057b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1106cbb2d20bea2861f244da2e2318d8c088300a3710Mathieu Chartier  new_array = gRegistry->Add(mirror::Array::Alloc<true>(Thread::Current(), c, length));
1107436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1108872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1109872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
111088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesbool Dbg::MatchType(JDWP::RefTypeId instance_class_id, JDWP::RefTypeId class_id) {
1111436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
11122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c1 = DecodeClass(instance_class_id, status);
1113a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  CHECK(c1 != NULL);
11142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c2 = DecodeClass(class_id, status);
1115a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  CHECK(c2 != NULL);
1116a656a0f6fbcf2ba3f15cae54a773b9c636dd32c1Elliott Hughes  return c1->IsAssignableFrom(c2);
1117872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1118872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1119ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::FieldId ToFieldId(const mirror::ArtField* f)
1120b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1121590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
112203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::FieldId>(reinterpret_cast<uintptr_t>(f));
112303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
112403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1125ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic JDWP::MethodId ToMethodId(const mirror::ArtMethod* m)
1126b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1127590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
112803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  return static_cast<JDWP::MethodId>(reinterpret_cast<uintptr_t>(m));
112903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
113003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1131ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtField* FromFieldId(JDWP::FieldId fid)
1132b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1133590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingFields);
1134ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtField*>(static_cast<uintptr_t>(fid));
1135aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes}
1136aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1137ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic mirror::ArtMethod* FromMethodId(JDWP::MethodId mid)
1138b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1139590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier  CHECK(!kMovingMethods);
1140ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  return reinterpret_cast<mirror::ArtMethod*>(static_cast<uintptr_t>(mid));
114103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes}
114203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
1143ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic void SetLocation(JDWP::JdwpLocation& location, mirror::ArtMethod* m, uint32_t dex_pc)
1144b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
114591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (m == NULL) {
114691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    memset(&location, 0, sizeof(location));
114791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  } else {
11482dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = m->GetDeclaringClass();
1149748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    location.type_tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
1150748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    location.class_id = gRegistry->Add(c);
1151748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    location.method_id = ToMethodId(m);
11520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    location.dex_pc = dex_pc;
115391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
1154d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes}
1155d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
1156a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetMethodName(JDWP::MethodId method_id)
1157b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1158ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
11596d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  return MethodHelper(m).GetName();
1160872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1161872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1162a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughesstd::string Dbg::GetFieldName(JDWP::FieldId field_id)
1163a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1164ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
1165a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes  return FieldHelper(f).GetName();
1166a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes}
1167a96836a4115ad08762567c10bd4d198c5b644985Elliott Hughes
1168a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes/*
1169a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * Augment the access flags for synthetic methods and fields by setting
1170a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * the (as described by the spec) "0xf0000000 bit".  Also, strip out any
1171a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes * flags not specified by the Java programming language.
1172a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes */
1173a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughesstatic uint32_t MangleAccessFlags(uint32_t accessFlags) {
1174a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  accessFlags &= kAccJavaFlagsMask;
1175a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  if ((accessFlags & kAccSynthetic) != 0) {
1176a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    accessFlags |= 0xf0000000;
1177a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1178a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return accessFlags;
1179872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1180872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1181dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes/*
1182b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come first. Debuggers
1183b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * expect slots to begin with arguments, but dex code places them at
1184b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * the end.
1185dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes */
1186b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Haostatic uint16_t MangleSlot(uint16_t slot, mirror::ArtMethod* m)
1187b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1188b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
1189b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1190b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1191b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot >= locals_size) {
1192b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - locals_size;
1193b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1194b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + ins_size;
1195dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1196dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1197dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1198b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao/*
1199b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * Circularly shifts registers so that arguments come last. Reverts
1200b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao * slots to dex style argument placement.
1201b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao */
1202ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromstatic uint16_t DemangleSlot(uint16_t slot, mirror::ArtMethod* m)
1203b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1204b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  const DexFile::CodeItem* code_item = MethodHelper(m).GetCodeItem();
1205b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t ins_size = code_item->ins_size_;
1206b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  uint16_t locals_size = code_item->registers_size_ - ins_size;
1207b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  if (slot < ins_size) {
1208b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot + locals_size;
1209b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  } else {
1210b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    return slot - ins_size;
1211dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  }
1212dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes}
1213dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
121488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredFields(JDWP::RefTypeId class_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1215436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
12162dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1217436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1218436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
12197b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1220a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1221a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t instance_field_count = c->NumInstanceFields();
1222a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t static_field_count = c->NumStaticFields();
1223a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1224a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, instance_field_count + static_field_count);
1225a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1226a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < instance_field_count + static_field_count; ++i) {
1227ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtField* f = (i < instance_field_count) ? c->GetInstanceField(i) : c->GetStaticField(i - instance_field_count);
12286d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    FieldHelper fh(f);
1229a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddFieldId(pReply, ToFieldId(f));
12306d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, fh.GetName());
12316d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, fh.GetTypeDescriptor());
1232c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1233a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1234a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1235a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1236a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(f->GetAccessFlags()));
1237a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1238436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1239a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1240a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
124188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredMethods(JDWP::RefTypeId class_id, bool with_generic,
124200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                           JDWP::ExpandBuf* pReply) {
1243436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
12442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1245436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1246436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
12477b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1248a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1249a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t direct_method_count = c->NumDirectMethods();
1250a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  size_t virtual_method_count = c->NumVirtualMethods();
1251a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1252a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, direct_method_count + virtual_method_count);
1253a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
1254a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) {
1255ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
12566d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    MethodHelper mh(m);
1257a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAddMethodId(pReply, ToMethodId(m));
12586d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers    expandBufAddUtf8String(pReply, mh.GetName());
1259d91d6d6a80748f277fd938a412211e5af28913b1Ian Rogers    expandBufAddUtf8String(pReply, mh.GetSignature().ToString());
1260c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    if (with_generic) {
1261a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      static const char genericSignature[1] = "";
1262a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes      expandBufAddUtf8String(pReply, genericSignature);
1263a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1264a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    expandBufAdd4BE(pReply, MangleAccessFlags(m->GetAccessFlags()));
1265a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1266436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1267a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes}
1268a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes
126988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::OutputDeclaredInterfaces(JDWP::RefTypeId class_id, JDWP::ExpandBuf* pReply) {
1270436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  JDWP::JdwpError status;
12712dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(class_id, status);
1272436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  if (c == NULL) {
1273436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes    return status;
12747b3cdfcca472b779cf8745fb8460935e56229f11Elliott Hughes  }
1275436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes
1276436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  ClassHelper kh(c);
1277d24e264ff85ad8c6f142ac6d33055fdc1881fa2fIan Rogers  size_t interface_count = kh.NumDirectInterfaces();
1278a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  expandBufAdd4BE(pReply, interface_count);
1279a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  for (size_t i = 0; i < interface_count; ++i) {
128064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    expandBufAddRefTypeId(pReply, gRegistry->AddRefType(kh.GetDirectInterface(i)));
1281a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1282436e372e1c2b8e0bde47408e7407b4c3590f5071Elliott Hughes  return JDWP::ERR_NONE;
1283872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1284872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
128588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputLineTable(JDWP::RefTypeId, JDWP::MethodId method_id, JDWP::ExpandBuf* pReply)
1286b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
128703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  struct DebugCallbackContext {
128803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    int numItems;
128903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    JDWP::ExpandBuf* pReply;
129003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
12912435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    static bool Callback(void* context, uint32_t address, uint32_t line_number) {
129203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
129303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      expandBufAdd8BE(pContext->pReply, address);
12942435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      expandBufAdd4BE(pContext->pReply, line_number);
129503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      pContext->numItems++;
1296f2910eef247b45ce1d489e323b36b5de6b6157aaSebastien Hertz      return false;
129703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
129803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
1299ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
13006d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  MethodHelper mh(m);
130103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  uint64_t start, end;
130203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  if (m->IsNative()) {
130303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = -1;
130403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    end = -1;
130503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  } else {
130603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    start = 0;
130714f0db92225d34622fa5cb1a6dc9287334aaf6c7jeffhao    // Return the index of the last instruction
130814f0db92225d34622fa5cb1a6dc9287334aaf6c7jeffhao    end = mh.GetCodeItem()->insns_size_in_code_units_ - 1;
130903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  }
131003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
131103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, start);
131203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd8BE(pReply, end);
131303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
131403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  // Add numLines later
131503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  size_t numLinesOffset = expandBufGetLength(pReply);
131603181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  expandBufAdd4BE(pReply, 0);
131703181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
131803181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  DebugCallbackContext context;
131903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.numItems = 0;
132003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  context.pReply = pReply;
132103181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
13226d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(),
13236d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers                                  DebugCallbackContext::Callback, NULL, &context);
132403181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes
132503181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + numLinesOffset, context.numItems);
1326872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1327872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
132888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::OutputVariableTable(JDWP::RefTypeId, JDWP::MethodId method_id, bool with_generic, JDWP::ExpandBuf* pReply) {
1329dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  struct DebugCallbackContext {
1330b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    mirror::ArtMethod* method;
1331dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    JDWP::ExpandBuf* pReply;
1332c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    size_t variable_count;
1333c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes    bool with_generic;
1334dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1335b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao    static void Callback(void* context, uint16_t slot, uint32_t startAddress, uint32_t endAddress, const char* name, const char* descriptor, const char* signature)
1336b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1337dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      DebugCallbackContext* pContext = reinterpret_cast<DebugCallbackContext*>(context);
1338dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1339b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao      VLOG(jdwp) << StringPrintf("    %2zd: %d(%d) '%s' '%s' '%s' actual slot=%d mangled slot=%d", pContext->variable_count, startAddress, endAddress - startAddress, name, descriptor, signature, slot, MangleSlot(slot, pContext->method));
1340dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1341b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao      slot = MangleSlot(slot, pContext->method);
134268fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
1343dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd8BE(pContext->pReply, startAddress);
1344dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, name);
1345dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAddUtf8String(pContext->pReply, descriptor);
1346c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      if (pContext->with_generic) {
1347dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes        expandBufAddUtf8String(pContext->pReply, signature);
1348dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      }
1349dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, endAddress - startAddress);
1350dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes      expandBufAdd4BE(pContext->pReply, slot);
1351dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1352c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes      ++pContext->variable_count;
1353dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes    }
1354dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  };
1355ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
13566d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  MethodHelper mh(m);
13576d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  const DexFile::CodeItem* code_item = mh.GetCodeItem();
1358dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1359c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // arg_count considers doubles and longs to take 2 units.
1360c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // variable_count considers everything to take 1 unit.
13616d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  std::string shorty(mh.GetShorty());
1362ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  expandBufAdd4BE(pReply, mirror::ArtMethod::NumArgRegisters(shorty));
1363dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1364c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  // We don't know the total number of variables yet, so leave a blank and update it later.
1365c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  size_t variable_count_offset = expandBufGetLength(pReply);
1366dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  expandBufAdd4BE(pReply, 0);
1367dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1368dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  DebugCallbackContext context;
1369b7cefc7f5cac99a62fd4e662c1bdeec750434e28Jeff Hao  context.method = m;
1370dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes  context.pReply = pReply;
1371c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.variable_count = 0;
1372c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  context.with_generic = with_generic;
1373dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
13746d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  mh.GetDexFile().DecodeDebugInfo(code_item, m->IsStatic(), m->GetDexMethodIndex(), NULL,
13756d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers                                  DebugCallbackContext::Callback, &context);
1376dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
1377c5b734a27a64c81590015dd12a7901c396818a99Elliott Hughes  JDWP::Set4BE(expandBufGetBuffer(pReply) + variable_count_offset, context.variable_count);
1378872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1379872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1380579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputMethodReturnValue(JDWP::MethodId method_id, const JValue* return_value,
1381579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                                  JDWP::ExpandBuf* pReply) {
1382579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  mirror::ArtMethod* m = FromMethodId(method_id);
1383579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  JDWP::JdwpTag tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty());
1384579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  OutputJValue(tag, return_value, pReply);
1385579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1386579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
13879777ba230c83a0edcbda2cf7b208339e77bf171bElliott HughesJDWP::JdwpError Dbg::GetBytecodes(JDWP::RefTypeId, JDWP::MethodId method_id,
13889777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes                                  std::vector<uint8_t>& bytecodes)
13899777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1390ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(method_id);
13919777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  if (m == NULL) {
13929777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    return JDWP::ERR_INVALID_METHODID;
13939777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
13949777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  MethodHelper mh(m);
13959777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const DexFile::CodeItem* code_item = mh.GetCodeItem();
13969777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  size_t byte_count = code_item->insns_size_in_code_units_ * 2;
13979777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* begin = reinterpret_cast<const uint8_t*>(code_item->insns_);
13989777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  const uint8_t* end = begin + byte_count;
13999777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  for (const uint8_t* p = begin; p != end; ++p) {
14009777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes    bytecodes.push_back(*p);
14019777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  }
14029777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes  return JDWP::ERR_NONE;
14039777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes}
14049777ba230c83a0edcbda2cf7b208339e77bf171bElliott Hughes
140588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetFieldBasicTag(JDWP::FieldId field_id) {
140688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
1407872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1408872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
140988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpTag Dbg::GetStaticFieldBasicTag(JDWP::FieldId field_id) {
141088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return BasicTagFromDescriptor(FieldHelper(FromFieldId(field_id)).GetTypeDescriptor());
1411872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1412872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
141388d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError GetFieldValueImpl(JDWP::RefTypeId ref_type_id, JDWP::ObjectId object_id,
141488d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                         JDWP::FieldId field_id, JDWP::ExpandBuf* pReply,
141500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         bool is_static)
1416b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
14170cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  JDWP::JdwpError status;
14182dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = DecodeClass(ref_type_id, status);
141988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  if (ref_type_id != 0 && c == NULL) {
14200cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    return status;
14210cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
14220cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
14232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
142464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
14253f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
14263f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1427ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
14280cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
14292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* receiver_class = c;
14300cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class == NULL && o != NULL) {
14310cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    receiver_class = o->GetClass();
14320cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
14330cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we give up now if receiver_class is NULL?
14340cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (receiver_class != NULL && !f->GetDeclaringClass()->IsAssignableFrom(receiver_class)) {
14350cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    LOG(INFO) << "ERR_INVALID_FIELDID: " << PrettyField(f) << " " << PrettyClass(receiver_class);
14363f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_FIELDID;
14373f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1438aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
14390cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
14400cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
14410cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
14420cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
14430cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
14440cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
14450cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
14460cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
14470cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
14480cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
14490cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  }
14500dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
14510dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
14520dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
14530cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
14546d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
1455579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  JValue field_value;
1456579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (tag == JDWP::JT_VOID) {
1457579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    LOG(FATAL) << "Unknown tag: " << tag;
1458579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (!IsPrimitiveTag(tag)) {
1459579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetL(f->GetObject(o));
1460579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1461579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetJ(f->Get64(o));
1462aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
1463579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    field_value.SetI(f->Get32(o));
1464aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
1465579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  Dbg::OutputJValue(tag, &field_value, pReply);
1466579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
14673f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  return JDWP::ERR_NONE;
1468872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1469872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
147088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id,
147100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ExpandBuf* pReply) {
147288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(0, object_id, field_id, pReply, false);
14733f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
14743f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
147588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::GetStaticFieldValue(JDWP::RefTypeId ref_type_id, JDWP::FieldId field_id, JDWP::ExpandBuf* pReply) {
147688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return GetFieldValueImpl(ref_type_id, 0, field_id, pReply, true);
14773f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes}
14783f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes
147988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstatic JDWP::JdwpError SetFieldValueImpl(JDWP::ObjectId object_id, JDWP::FieldId field_id,
148000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                         uint64_t value, int width, bool is_static)
1481b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
14822dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* o = gRegistry->Get<mirror::Object*>(object_id);
148364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if ((!is_static && o == NULL) || o == ObjectRegistry::kInvalidObject) {
14843f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
14853f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
1486ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = FromFieldId(field_id);
14870cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes
14880cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // The RI only enforces the static/non-static mismatch in one direction.
14890cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  // TODO: should we change the tests and check both?
14900cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  if (is_static) {
14910cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (!f->IsStatic()) {
14920cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      return JDWP::ERR_INVALID_FIELDID;
14930cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
14940cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes  } else {
14950cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    if (f->IsStatic()) {
14960cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes      LOG(WARNING) << "Ignoring non-NULL receiver for ObjectReference.SetValues on static field " << PrettyField(f);
14970cf7433fd2a93b7f83e8b2ac91d0d1b4f0e7d0c9Elliott Hughes    }
14983f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes  }
14990dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  if (f->IsStatic()) {
15000dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    o = f->GetDeclaringClass();
15010dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao  }
1502aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
15036d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  JDWP::JdwpTag tag = BasicTagFromDescriptor(FieldHelper(f).GetTypeDescriptor());
1504aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes
1505aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  if (IsPrimitiveTag(tag)) {
1506aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
15071bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_EQ(width, 8);
1508aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes      f->Set64(o, value);
1509aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    } else {
15101bac54ffa933fbe9b92b62437577f2f4583eff1aElliott Hughes      CHECK_LE(width, 4);
1511aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes      f->Set32(o, value);
1512aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes    }
1513aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  } else {
15142dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* v = gRegistry->Get<mirror::Object*>(value);
151564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (v == ObjectRegistry::kInvalidObject) {
15163d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes      return JDWP::ERR_INVALID_OBJECT;
15173d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    }
15183f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    if (v != NULL) {
15192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* field_type = FieldHelper(f).GetType();
15203f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      if (!field_type->IsAssignableFrom(v->GetClass())) {
15213f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes        return JDWP::ERR_INVALID_OBJECT;
15223f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      }
15233f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
15243d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes    f->SetObject(o, v);
1525aed4be94da51b4fbb54c728151f0daf11535f6abElliott Hughes  }
15263d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes
15273d1ca6de2ddf3d9e39755d629c896bfecd9640fdElliott Hughes  return JDWP::ERR_NONE;
1528872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1529872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
153088d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetFieldValue(JDWP::ObjectId object_id, JDWP::FieldId field_id, uint64_t value,
153100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   int width) {
153288d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(object_id, field_id, value, width, false);
1533872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1534872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
153588d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::SetStaticFieldValue(JDWP::FieldId field_id, uint64_t value, int width) {
153688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes  return SetFieldValueImpl(0, field_id, value, width, true);
1537872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1538872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
153988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::StringToUtf8(JDWP::ObjectId string_id) {
15402dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = gRegistry->Get<mirror::String*>(string_id);
154168fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  return s->ToModifiedUtf8();
1542872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1543872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1544579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::OutputJValue(JDWP::JdwpTag tag, const JValue* return_value, JDWP::ExpandBuf* pReply) {
1545579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  if (IsPrimitiveTag(tag)) {
1546579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAdd1(pReply, tag);
1547579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    if (tag == JDWP::JT_BOOLEAN || tag == JDWP::JT_BYTE) {
1548579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd1(pReply, return_value->GetI());
1549579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_CHAR || tag == JDWP::JT_SHORT) {
1550579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd2BE(pReply, return_value->GetI());
1551579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_FLOAT || tag == JDWP::JT_INT) {
1552579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd4BE(pReply, return_value->GetI());
1553579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else if (tag == JDWP::JT_DOUBLE || tag == JDWP::JT_LONG) {
1554579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      expandBufAdd8BE(pReply, return_value->GetJ());
1555579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    } else {
1556579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao      CHECK_EQ(tag, JDWP::JT_VOID);
1557579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    }
1558579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  } else {
1559579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    mirror::Object* value = return_value->GetL();
1560579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAdd1(pReply, TagFromObject(value));
1561579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    expandBufAddObjectId(pReply, gRegistry->Add(value));
1562579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  }
1563579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao}
1564579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao
1565221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadName(JDWP::ObjectId thread_id, std::string& name) {
1566a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  ScopedObjectAccessUnchecked soa(Thread::Current());
1567a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1568221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1569221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1570221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE && error != JDWP::ERR_THREAD_NOT_ALIVE) {
1571221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1572a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  }
1573221229cb523f849f165fdafbf9785010963715daElliott Hughes
1574221229cb523f849f165fdafbf9785010963715daElliott Hughes  // We still need to report the zombie threads' names, so we can't just call Thread::GetThreadName.
15752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
1576ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* java_lang_Thread_name_field =
15772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      soa.DecodeField(WellKnownClasses::java_lang_Thread_name);
15782dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s =
15792dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      reinterpret_cast<mirror::String*>(java_lang_Thread_name_field->GetObject(thread_object));
1580221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (s != NULL) {
1581221229cb523f849f165fdafbf9785010963715daElliott Hughes    name = s->ToModifiedUtf8();
1582221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1583221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1584872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1585872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1586221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadGroup(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
158700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
15882dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_object = gRegistry->Get<mirror::Object*>(thread_id);
158964f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (thread_object == ObjectRegistry::kInvalidObject) {
15902435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    return JDWP::ERR_INVALID_OBJECT;
15912435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
15922435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
15932435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Okay, so it's an object, but is it actually a thread?
159450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1595221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1596221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1597221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1598221229cb523f849f165fdafbf9785010963715daElliott Hughes    // Zombie threads are in the null group.
1599221229cb523f849f165fdafbf9785010963715daElliott Hughes    expandBufAddObjectId(pReply, JDWP::ObjectId(0));
1600221229cb523f849f165fdafbf9785010963715daElliott Hughes    return JDWP::ERR_NONE;
1601221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1602221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1603221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
16042435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
1605499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
16062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/Thread;");
1607499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(c != NULL);
1608ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("group", "Ljava/lang/ThreadGroup;");
1609499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(f != NULL);
16102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(thread_object);
1611499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(group != NULL);
16122435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  JDWP::ObjectId thread_group_id = gRegistry->Add(group);
16132435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
16142435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  expandBufAddObjectId(pReply, thread_group_id);
16152435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1616872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1617872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
161888d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesstd::string Dbg::GetThreadGroupName(JDWP::ObjectId thread_group_id) {
161900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
16202dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
1621499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(thread_group != NULL);
1622499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
16232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;");
1624499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(c != NULL);
1625ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("name", "Ljava/lang/String;");
1626499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  CHECK(f != NULL);
16272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::String* s = reinterpret_cast<mirror::String*>(f->GetObject(thread_group));
1628499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  return s->ToModifiedUtf8();
1629872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1630872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
163188d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::ObjectId Dbg::GetThreadGroupParent(JDWP::ObjectId thread_group_id) {
16322dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
16334e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  CHECK(thread_group != NULL);
16344e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes
16352dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = Runtime::Current()->GetClassLinker()->FindSystemClass("Ljava/lang/ThreadGroup;");
16364e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  CHECK(c != NULL);
1637ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = c->FindInstanceField("parent", "Ljava/lang/ThreadGroup;");
16384e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  CHECK(f != NULL);
16392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* parent = f->GetObject(thread_group);
16404e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  return gRegistry->Add(parent);
1641872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1642872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1643872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetSystemThreadGroupId() {
164400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
1645ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_systemThreadGroup);
16462dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
1647365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
1648872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1649872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1650872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetMainThreadGroupId() {
165100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1652ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* f = soa.DecodeField(WellKnownClasses::java_lang_ThreadGroup_mainThreadGroup);
16532dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* group = f->GetObject(f->GetDeclaringClass());
1654365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  return gRegistry->Add(group);
1655872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1656872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1657920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff HaoJDWP::JdwpThreadStatus Dbg::ToJdwpThreadStatus(ThreadState state) {
1658920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  switch (state) {
1659920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kBlocked:
1660920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_MONITOR;
1661920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kNative:
1662920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kRunnable:
1663920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSuspended:
1664920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_RUNNING;
1665920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kSleeping:
1666920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_SLEEPING;
1667920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kStarting:
1668920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTerminated:
1669920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_ZOMBIE;
1670920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kTimedWaiting:
1671920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSend:
1672920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerSuspension:
1673920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForDebuggerToAttach:
1674920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForGcToComplete:
1675920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForCheckPointsToRun:
1676920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForJniOnLoad:
1677920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingForSignalCatcherOutput:
1678920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainDebuggerLoop:
1679920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingInMainSignalCatcherLoop:
1680920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaitingPerformingGc:
1681920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao    case kWaiting:
1682920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      return JDWP::TS_WAIT;
1683920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao      // Don't add a 'default' here so the compiler can spot incompatible enum changes.
1684920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  }
1685920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  LOG(FATAL) << "Unknown thread state: " << state;
1686920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  return JDWP::TS_ZOMBIE;
1687920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao}
1688920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao
1689221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadStatus(JDWP::ObjectId thread_id, JDWP::JdwpThreadStatus* pThreadStatus, JDWP::JdwpSuspendStatus* pSuspendStatus) {
169000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1691499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
16929e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  *pSuspendStatus = JDWP::SUSPEND_STATUS_NOT_SUSPENDED;
16939e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes
169450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1695221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1696221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1697221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1698221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error == JDWP::ERR_THREAD_NOT_ALIVE) {
1699221229cb523f849f165fdafbf9785010963715daElliott Hughes      *pThreadStatus = JDWP::TS_ZOMBIE;
1700221229cb523f849f165fdafbf9785010963715daElliott Hughes      return JDWP::ERR_NONE;
1701221229cb523f849f165fdafbf9785010963715daElliott Hughes    }
1702221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1703499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes  }
1704499c5133d361e7c659fc38e5ccfeb1280a7996f5Elliott Hughes
17059e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  if (IsSuspendedForDebugger(soa, thread)) {
17069e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    *pSuspendStatus = JDWP::SUSPEND_STATUS_SUSPENDED;
17079e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes  }
170800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
1709920af3e556c730a5fbdab90a6d0ec1a2dbe8940bJeff Hao  *pThreadStatus = ToJdwpThreadStatus(thread->GetState());
1710221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
1711872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1712872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1713221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadDebugSuspendCount(JDWP::ObjectId thread_id, JDWP::ExpandBuf* pReply) {
171400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
171550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1716221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1717221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1718221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1719221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
17202435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
172150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
172200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  expandBufAdd4BE(pReply, thread->GetDebugSuspendCount());
17232435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
1724872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1725872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1726f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott HughesJDWP::JdwpError Dbg::Interrupt(JDWP::ObjectId thread_id) {
1727f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  ScopedObjectAccess soa(Thread::Current());
1728f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1729f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  Thread* thread;
1730f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1731f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  if (error != JDWP::ERR_NONE) {
1732f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes    return error;
1733f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  }
1734f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  thread->Interrupt();
1735f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes  return JDWP::ERR_NONE;
1736f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes}
1737f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes
1738caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughesvoid Dbg::GetThreads(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& thread_ids) {
1739365c10235438607541fa2259a5fec48061b90bd8Ian Rogers  class ThreadListVisitor {
1740365c10235438607541fa2259a5fec48061b90bd8Ian Rogers   public:
17412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    ThreadListVisitor(const ScopedObjectAccessUnchecked& soa, mirror::Object* desired_thread_group,
174200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                      std::vector<JDWP::ObjectId>& thread_ids)
1743b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
17440dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        : soa_(soa), desired_thread_group_(desired_thread_group), thread_ids_(thread_ids) {}
1745365c10235438607541fa2259a5fec48061b90bd8Ian Rogers
1746a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    static void Visit(Thread* t, void* arg) {
1747a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      reinterpret_cast<ThreadListVisitor*>(arg)->Visit(t);
1748a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1749a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
175000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
175100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
175200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    void Visit(Thread* t) NO_THREAD_SAFETY_ANALYSIS {
1753a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (t == Dbg::GetDebugThread()) {
1754a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        // Skip the JDWP thread. Some debuggers get bent out of shape when they can't suspend and
1755a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        // query all threads, so it's easier if we just don't tell them about this thread.
1756a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
1757a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
17582dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* peer = t->GetPeer();
17590dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      if (IsInDesiredThreadGroup(peer)) {
1760120f1c74a9768e958377b6c97897511b27ae58c8Ian Rogers        thread_ids_.push_back(gRegistry->Add(peer));
1761a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
1762a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
1763a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
1764365c10235438607541fa2259a5fec48061b90bd8Ian Rogers   private:
17652dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    bool IsInDesiredThreadGroup(mirror::Object* peer)
17660dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
17670dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      // peer might be NULL if the thread is still starting up.
17680dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      if (peer == NULL) {
17690dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // We can't tell the debugger about this thread yet.
17700dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // TODO: if we identified threads to the debugger by their Thread*
17712dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        // rather than their peer's mirror::Object*, we could fix this.
17720dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        // Doing so might help us report ZOMBIE threads too.
17730dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao        return false;
17740dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      }
1775c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      // Do we want threads from all thread groups?
1776c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      if (desired_thread_group_ == NULL) {
1777c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao        return true;
1778c1e0490a2a0293fdfc5f654482339ccc71d9952bjeffhao      }
17792dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* group = soa_.DecodeField(WellKnownClasses::java_lang_Thread_group)->GetObject(peer);
17800dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao      return (group == desired_thread_group_);
17810dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao    }
17820dfbb7ead0b6da92911b4bc78114ff0f0f9b4129jeffhao
1783dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier    const ScopedObjectAccessUnchecked& soa_;
17842dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* const desired_thread_group_;
1785caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    std::vector<JDWP::ObjectId>& thread_ids_;
1786a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  };
1787a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
178800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
17892dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
179000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ThreadListVisitor tlv(soa, thread_group, thread_ids);
179150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1792f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughes  Runtime::Current()->GetThreadList()->ForEach(ThreadListVisitor::Visit, &tlv);
1793a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes}
1794a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
1795caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughesvoid Dbg::GetChildThreadGroups(JDWP::ObjectId thread_group_id, std::vector<JDWP::ObjectId>& child_thread_group_ids) {
179600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
17972dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* thread_group = gRegistry->Get<mirror::Object*>(thread_group_id);
1798caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes
1799caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Get the ArrayList<ThreadGroup> "groups" out of this thread group...
1800ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* groups_field = thread_group->GetClass()->FindInstanceField("groups", "Ljava/util/List;");
18012dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* groups_array_list = groups_field->GetObject(thread_group);
1802872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1803caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Get the array and size out of the ArrayList<ThreadGroup>...
1804ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* array_field = groups_array_list->GetClass()->FindInstanceField("array", "[Ljava/lang/Object;");
1805ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtField* size_field = groups_array_list->GetClass()->FindInstanceField("size", "I");
18062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::ObjectArray<mirror::Object>* groups_array =
18072dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      array_field->GetObject(groups_array_list)->AsObjectArray<mirror::Object>();
1808caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  const int32_t size = size_field->GetInt(groups_array_list);
1809caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes
1810caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  // Copy the first 'size' elements out of the array into the result.
1811caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  for (int32_t i = 0; i < size; ++i) {
1812caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes    child_thread_group_ids.push_back(gRegistry->Add(groups_array->Get(i)));
1813caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  }
1814872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1815872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
181600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic int GetStackDepth(Thread* thread)
1817b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
18180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct CountStackDepthVisitor : public StackVisitor {
181993ba893c20532990a430741e0a97212900094e8cBrian Carlstrom    explicit CountStackDepthVisitor(Thread* thread)
18207a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth(0) {}
18210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
182264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
182364f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    // annotalysis.
182464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
18250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!GetMethod()->IsRuntimeMethod()) {
1826f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes        ++depth;
1827f8a2df7bbf1021058bc13d1f806a7fec3c89ee62Elliott Hughes      }
1828530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
1829a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    }
1830a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes    size_t depth;
1831a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  };
183208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
18337a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  CountStackDepthVisitor visitor(thread);
18340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
1835a2e54f61453a2072181d5dd7aa2d5e845f5b53f6Elliott Hughes  return visitor.depth;
1836872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1837872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1838221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::GetThreadFrameCount(JDWP::ObjectId thread_id, size_t& result) {
183900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
1840a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1841221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1842221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1843221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1844221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1845221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1846f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
1847f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
1848f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
1849221229cb523f849f165fdafbf9785010963715daElliott Hughes  result = GetStackDepth(thread);
1850221229cb523f849f165fdafbf9785010963715daElliott Hughes  return JDWP::ERR_NONE;
18518696433d1b3d8ba15288483b777edd888de69135Elliott Hughes}
18528696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
1853306057fd278d75bf3794bd5243a3b6652c487d18Ian RogersJDWP::JdwpError Dbg::GetThreadFrames(JDWP::ObjectId thread_id, size_t start_frame,
1854306057fd278d75bf3794bd5243a3b6652c487d18Ian Rogers                                     size_t frame_count, JDWP::ExpandBuf* buf) {
18556e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  class GetFrameVisitor : public StackVisitor {
18566e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   public:
18577a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    GetFrameVisitor(Thread* thread, size_t start_frame, size_t frame_count, JDWP::ExpandBuf* buf)
1858b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
18597a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, NULL), depth_(0),
18606e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes          start_frame_(start_frame), frame_count_(frame_count), buf_(buf) {
18616e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      expandBufAdd4BE(buf_, frame_count_);
186203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
18630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
186400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
186500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
186600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
18670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetMethod()->IsRuntimeMethod()) {
18687934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom        return true;  // The debugger can't do anything useful with a frame that has no Method*.
186903181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
18706e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_ + frame_count_) {
1871530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes        return false;
187203181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes      }
18736e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      if (depth_ >= start_frame_) {
18746e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::FrameId frame_id(GetFrameId());
18756e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        JDWP::JdwpLocation location;
18766e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        SetLocation(location, GetMethod(), GetDexPc());
18777baf96f592522b763d15389a01c6ab6f0a977621Elliott Hughes        VLOG(jdwp) << StringPrintf("    Frame %3zd: id=%3lld ", depth_, frame_id) << location;
18786e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAdd8BE(buf_, frame_id);
18796e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes        expandBufAddLocation(buf_, location);
18806e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      }
18816e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes      ++depth_;
1882530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
188303181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes    }
18846e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes
18856e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes   private:
18866e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    size_t depth_;
18876e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t start_frame_;
18886e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    const size_t frame_count_;
18896e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    JDWP::ExpandBuf* buf_;
189003181a828cd493545b278e0aa4f150fdaf1e3325Elliott Hughes  };
189100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
189200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
1893a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1894221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
1895221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1896221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
1897221229cb523f849f165fdafbf9785010963715daElliott Hughes    return error;
1898221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
1899f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  if (!IsSuspendedForDebugger(soa, thread)) {
1900f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes    return JDWP::ERR_THREAD_NOT_SUSPENDED;
1901f15f4a0136a7b797117355768851bc421b3afc93Elliott Hughes  }
19027a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetFrameVisitor visitor(thread, start_frame, frame_count, buf);
19030399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
19046e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
1905872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1906872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1907872d4ec7225444d9400d30f9027247deb91012fdElliott HughesJDWP::ObjectId Dbg::GetThreadSelfId() {
1908dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier  ScopedObjectAccessUnchecked soa(Thread::Current());
1909cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers  return gRegistry->Add(soa.Self()->GetPeer());
1910872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1911872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1912475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughesvoid Dbg::SuspendVM() {
191300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Runtime::Current()->GetThreadList()->SuspendAllForDebugger();
1914872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1915872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1916872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ResumeVM() {
1917c61a267e98ed1038b74c33c7740414ced4a27d89Elliott Hughes  Runtime::Current()->GetThreadList()->UndoDebuggerSuspensions();
1918872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1919872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1920221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::SuspendThread(JDWP::ObjectId thread_id, bool request_suspension) {
192100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedLocalRef<jobject> peer(Thread::Current()->GetJniEnv(), NULL);
192200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
192300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccess soa(Thread::Current());
19242dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    peer.reset(soa.AddLocalReference<jobject>(gRegistry->Get<mirror::Object*>(thread_id)));
192500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
192600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (peer.get() == NULL) {
192700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
192800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
192900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Suspend thread to build stack trace.
1930f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  bool timed_out;
1931d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  Thread* thread = ThreadList::SuspendThreadByPeer(peer.get(), request_suspension, true,
1932d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                                   &timed_out);
193300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (thread != NULL) {
193400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_NONE;
1935f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes  } else if (timed_out) {
193600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_INTERNAL;
193700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  } else {
193800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    return JDWP::ERR_THREAD_NOT_ALIVE;
19394e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
1940872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1941872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1942221229cb523f849f165fdafbf9785010963715daElliott Hughesvoid Dbg::ResumeThread(JDWP::ObjectId thread_id) {
194300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
19442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* peer = gRegistry->Get<mirror::Object*>(thread_id);
1945a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  Thread* thread;
1946a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  {
1947a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1948a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao    thread = Thread::FromManagedThread(soa, peer);
1949a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  }
19504e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  if (thread == NULL) {
19514e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    LOG(WARNING) << "No such thread for resume: " << peer;
19524e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes    return;
19534e235316b4ada4d865c544d9b4ba397139333fa6Elliott Hughes  }
195400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool needs_resume;
195500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
195650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
195700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    needs_resume = thread->GetSuspendCount() > 0;
195800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  }
195900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  if (needs_resume) {
1960546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes    Runtime::Current()->GetThreadList()->Resume(thread, true);
1961546b986ecd09c2a498740bd8bf7127e612d94755Elliott Hughes  }
1962872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1963872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
1964872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::SuspendSelf() {
1965475fc23a4a7f35d1be87ea0b06c80df317a720acElliott Hughes  Runtime::Current()->GetThreadList()->SuspendSelfForDebugger();
1966872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
1967872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
19680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct GetThisVisitor : public StackVisitor {
19697a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id)
1970b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
19717a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, context), this_object(NULL), frame_id(frame_id) {}
197268fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes
197300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
197400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
197500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  virtual bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
19766e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes    if (frame_id != GetFrameId()) {
19770399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return true;  // continue
19780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    } else {
197962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      this_object = GetThisObject();
198062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      return false;
19810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
198268fdbd07fc2b8856905e06f3cc945b046c3bfcd3Elliott Hughes  }
1983dbb4079eb1e7d7738c81a97c8dd2550885c1093aElliott Hughes
19842dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Object* this_object;
19856e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  JDWP::FrameId frame_id;
19860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers};
1987ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
198800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersJDWP::JdwpError Dbg::GetThisObject(JDWP::ObjectId thread_id, JDWP::FrameId frame_id,
198900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::ObjectId* result) {
199000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
199100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* thread;
199200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  {
199350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
1994221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
1995221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
1996221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
199700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
19989e0c175a0cea5c8c88a6927e6375554118f74a82Elliott Hughes    if (!IsSuspendedForDebugger(soa, thread)) {
199900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      return JDWP::ERR_THREAD_NOT_SUSPENDED;
200000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
20016e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  }
2002caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  UniquePtr<Context> context(Context::Create());
20037a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetThisVisitor visitor(thread, context.get(), frame_id);
20040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
20056e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  *result = gRegistry->Add(visitor.this_object);
20066e9d22c78c1016e70f8c2a9e6bc66c94ec36f728Elliott Hughes  return JDWP::ERR_NONE;
20070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
20080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
200988d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::GetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag,
201000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                        uint8_t* buf, size_t width) {
20110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct GetLocalVisitor : public StackVisitor {
20127a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    GetLocalVisitor(Thread* thread, Context* context, JDWP::FrameId frame_id, int slot,
20137a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers                    JDWP::JdwpTag tag, uint8_t* buf, size_t width)
2014b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
20157a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context), frame_id_(frame_id), slot_(slot), tag_(tag),
2016ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers          buf_(buf), width_(width) {}
2017ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
201800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
201900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
202000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
20210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetFrameId() != frame_id_) {
20220399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        return true;  // Not our frame, carry on.
20230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
20240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // TODO: check that the tag is compatible with the actual type of the slot!
2025ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* m = GetMethod();
20260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      uint16_t reg = DemangleSlot(slot_, m);
20270399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
20280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      switch (tag_) {
20290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_BOOLEAN:
20300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
20310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
20322bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
20330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get boolean local " << reg << " = " << intVal;
20340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set1(buf_+1, intVal != 0);
20350399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
20360399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
20370399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_BYTE:
20380399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
20390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
20402bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
20410399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get byte local " << reg << " = " << intVal;
20420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set1(buf_+1, intVal);
20430399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
20440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
20450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_SHORT:
20460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CHAR:
20470399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
20480399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 2U);
20492bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
20500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get short/char local " << reg << " = " << intVal;
20510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set2BE(buf_+1, intVal);
20520399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
20530399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
20540399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_INT:
20552bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        {
20562bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 4U);
20572bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kIntVReg);
20582bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          VLOG(jdwp) << "get int local " << reg << " = " << intVal;
20592bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          JDWP::Set4BE(buf_+1, intVal);
20602bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
20612bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        break;
20620399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_FLOAT:
20630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
20640399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 4U);
20652bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t intVal = GetVReg(m, reg, kFloatVReg);
20660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get int/float local " << reg << " = " << intVal;
20670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set4BE(buf_+1, intVal);
20680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
20690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
20700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_ARRAY:
20710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
20720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
20732dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
20740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get array local " << reg << " = " << o;
2075590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier          if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
20760399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            LOG(FATAL) << "Register " << reg << " expected to hold array: " << o;
20770399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
20780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
20790399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
20800399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
20810399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CLASS_LOADER:
20820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_CLASS_OBJECT:
20830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_OBJECT:
20840399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_STRING:
20850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_THREAD:
20860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_THREAD_GROUP:
20870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
20880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
20892dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = reinterpret_cast<mirror::Object*>(GetVReg(m, reg, kReferenceVReg));
20900399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get object local " << reg << " = " << o;
2091590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier          if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(o)) {
20920399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            LOG(FATAL) << "Register " << reg << " expected to hold object: " << o;
20930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
20940399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          tag_ = TagFromObject(o);
20950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::SetObjectId(buf_+1, gRegistry->Add(o));
20960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
20970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
20980399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_DOUBLE:
20992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        {
21002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 8U);
21012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t lo = GetVReg(m, reg, kDoubleLoVReg);
21022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t hi = GetVReg(m, reg + 1, kDoubleHiVReg);
21032bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t longVal = (hi << 32) | lo;
21042bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
21052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          JDWP::Set8BE(buf_+1, longVal);
21062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        }
21072bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers        break;
21080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      case JDWP::JT_LONG:
21090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
21100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 8U);
21112bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint32_t lo = GetVReg(m, reg, kLongLoVReg);
21122bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          uint64_t hi = GetVReg(m, reg + 1, kLongHiVReg);
21130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          uint64_t longVal = (hi << 32) | lo;
21140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          VLOG(jdwp) << "get double/long local " << hi << ":" << lo << " = " << longVal;
21150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          JDWP::Set8BE(buf_+1, longVal);
21160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
21170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
21180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      default:
21190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        LOG(FATAL) << "Unknown tag " << tag_;
21200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
21210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
2122ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes
21230399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // Prepend tag, which may have been updated.
21240399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      JDWP::Set1(buf_, tag_);
21250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return false;
21260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    }
21270399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
21280399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::FrameId frame_id_;
21290399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const int slot_;
21300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    JDWP::JdwpTag tag_;
21310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    uint8_t* const buf_;
21320399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const size_t width_;
21330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
213400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
213500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2136a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2137221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2138221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2139221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2140221229cb523f849f165fdafbf9785010963715daElliott Hughes    return;
2141221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
21420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  UniquePtr<Context> context(Context::Create());
21437a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  GetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, buf, width);
21440399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
21450399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers}
21460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
214788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughesvoid Dbg::SetLocalValue(JDWP::ObjectId thread_id, JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag,
21480399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                        uint64_t value, size_t width) {
21490399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SetLocalVisitor : public StackVisitor {
21507a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers    SetLocalVisitor(Thread* thread, Context* context,
21510399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers                    JDWP::FrameId frame_id, int slot, JDWP::JdwpTag tag, uint64_t value,
2152ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers                    size_t width)
2153b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
21547a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers        : StackVisitor(thread, context),
215508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes          frame_id_(frame_id), slot_(slot), tag_(tag), value_(value), width_(width) {}
2156ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
215700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
215800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
215900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
21600399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (GetFrameId() != frame_id_) {
21610399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        return true;  // Not our frame, carry on.
2162ad3da694bbba88662d1d1bd2cc574d6e3ab6cd42Elliott Hughes      }
21630399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      // TODO: check that the tag is compatible with the actual type of the slot!
2164ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      mirror::ArtMethod* m = GetMethod();
21650399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      uint16_t reg = DemangleSlot(slot_, m);
21660399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
21670399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      switch (tag_) {
21680399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_BOOLEAN:
21690399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_BYTE:
21700399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 1U);
21712bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
21720399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
21730399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_SHORT:
21740399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_CHAR:
21750399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 2U);
21762bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
21770399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
21780399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_INT:
21792bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 4U);
21802bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kIntVReg);
21812bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          break;
21820399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_FLOAT:
21830399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 4U);
21842bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kFloatVReg);
21850399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
21860399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_ARRAY:
21870399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_OBJECT:
21880399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_STRING:
21890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        {
21900399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, sizeof(JDWP::ObjectId));
21912dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Object* o = gRegistry->Get<mirror::Object*>(static_cast<JDWP::ObjectId>(value_));
219264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes          if (o == ObjectRegistry::kInvalidObject) {
21930399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers            UNIMPLEMENTED(FATAL) << "return an error code when given an invalid object to store";
21940399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          }
21952bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(reinterpret_cast<uintptr_t>(o)), kReferenceVReg);
21960399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        }
21970399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        break;
21980399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_DOUBLE:
21992bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          CHECK_EQ(width_, 8U);
22002bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kDoubleLoVReg);
22012bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kDoubleHiVReg);
22022bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          break;
22030399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        case JDWP::JT_LONG:
22040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          CHECK_EQ(width_, 8U);
22052bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg, static_cast<uint32_t>(value_), kLongLoVReg);
22062bcb4a496b7aa00d996df3a070524f7568fb35a1Ian Rogers          SetVReg(m, reg + 1, static_cast<uint32_t>(value_ >> 32), kLongHiVReg);
22070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
22080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers        default:
22090399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          LOG(FATAL) << "Unknown tag " << tag_;
22100399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers          break;
22110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      }
22120399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      return false;
2213cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    }
22140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers
22150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::FrameId frame_id_;
22160399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const int slot_;
22170399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const JDWP::JdwpTag tag_;
22180399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const uint64_t value_;
22190399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    const size_t width_;
22200399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  };
222100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
222200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccessUnchecked soa(Thread::Current());
2223a77f0f68a59870ff027dad654fe31eb3fdaf3d5djeffhao  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2224221229cb523f849f165fdafbf9785010963715daElliott Hughes  Thread* thread;
2225221229cb523f849f165fdafbf9785010963715daElliott Hughes  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
2226221229cb523f849f165fdafbf9785010963715daElliott Hughes  if (error != JDWP::ERR_NONE) {
2227221229cb523f849f165fdafbf9785010963715daElliott Hughes    return;
2228221229cb523f849f165fdafbf9785010963715daElliott Hughes  }
222908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  UniquePtr<Context> context(Context::Create());
22307a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  SetLocalVisitor visitor(thread, context.get(), frame_id, slot, tag, value, width);
22310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
2232872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2233872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2234579b02416e05e32e535126e1ed61613a2cdb030eJeff Haovoid Dbg::PostLocationEvent(const mirror::ArtMethod* m, int dex_pc, mirror::Object* this_object,
2235579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao                            int event_flags, const JValue* return_value) {
22362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* c = m->GetDeclaringClass();
223791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
223891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  JDWP::JdwpLocation location;
2239748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes  location.type_tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
224064f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  location.class_id = gRegistry->AddRefType(c);
2241748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes  location.method_id = ToMethodId(m);
2242972a47bf0c180dee7e4c697b74dd31daaf58e008Elliott Hughes  location.dex_pc = m->IsNative() ? -1 : dex_pc;
224391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
224464f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // If 'this_object' isn't already in the registry, we know that we're not looking for it,
224564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  // so there's no point adding it to the registry and burning through ids.
224664f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::ObjectId this_id = 0;
224764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  if (gRegistry->Contains(this_object)) {
224864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    this_id = gRegistry->Add(this_object);
224991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
2250579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao  gJdwpState->PostLocationEvent(&location, this_id, event_flags, return_value);
2251872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2252872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
225362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Dbg::PostException(Thread* thread, const ThrowLocation& throw_location,
2254ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                        mirror::ArtMethod* catch_method,
225564f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes                        uint32_t catch_dex_pc, mirror::Throwable* exception_object) {
2256c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
22570ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers    return;
22580ad5bb8ea378a223eb6eaf89e0be2823c6f87c0eIan Rogers  }
22594740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
226062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  JDWP::JdwpLocation jdwp_throw_location;
226162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SetLocation(jdwp_throw_location, throw_location.GetMethod(), throw_location.GetDexPc());
2262d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  JDWP::JdwpLocation catch_location;
2263caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes  SetLocation(catch_location, catch_method, catch_dex_pc);
2264d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2265d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // We need 'this' for InstanceOnly filters.
226662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  JDWP::ObjectId this_id = gRegistry->Add(throw_location.GetThis());
226764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::ObjectId exception_id = gRegistry->Add(exception_object);
226864f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes  JDWP::RefTypeId exception_class_id = gRegistry->AddRefType(exception_object->GetClass());
2269d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
227062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  gJdwpState->PostException(&jdwp_throw_location, exception_id, exception_class_id, &catch_location,
227162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                            this_id);
2272872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2273872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
22742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::PostClassPrepare(mirror::Class* c) {
2275c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (!IsDebuggerActive()) {
22764740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes    return;
22774740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  }
22784740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes
22793d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes  // OLD-TODO - we currently always send both "verified" and "prepared" since
22804740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  // debuggers seem to like that.  There might be some advantage to honesty,
22814740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  // since the class may not yet be verified.
22824740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  int state = JDWP::CS_VERIFIED | JDWP::CS_PREPARED;
22834740cdff25875c530649a670b15e8ac52bfd7252Elliott Hughes  JDWP::JdwpTypeTag tag = c->IsInterface() ? JDWP::TT_INTERFACE : JDWP::TT_CLASS;
2284fc0e94bed3f88ed7e50854fd8dfaf5dcb345250fIan Rogers  gJdwpState->PostClassPrepare(tag, gRegistry->Add(c),
2285dfb325e0ddd746cd8f7c2e3723b3a573eb7cc111Ian Rogers                               ClassHelper(c).GetDescriptor(), state);
2286872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2287872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
228862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersvoid Dbg::UpdateDebugger(Thread* thread, mirror::Object* this_object,
2289ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom                         const mirror::ArtMethod* m, uint32_t dex_pc) {
229062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  if (!IsDebuggerActive() || dex_pc == static_cast<uint32_t>(-2) /* fake method exit */) {
22912aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes    return;
229291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
229391bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
22942aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes  int event_flags = 0;
22952aa2e39548e194c5514d6534149ca1078021eab1Elliott Hughes
22968696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  if (IsBreakpoint(m, dex_pc)) {
22978696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    event_flags |= kBreakpoint;
229891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
229991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
230061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // If the debugger is single-stepping one of our threads, check to
230161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  // see if we're that thread and we've reached a step point.
230261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  const SingleStepControl* single_step_control = thread->GetSingleStepControl();
230361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
230461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (single_step_control->is_active) {
230561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    CHECK(!m->IsNative());
230661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    if (single_step_control->step_depth == JDWP::SD_INTO) {
230761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step into method calls.  We break when the line number
230861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // or method pointer changes.  If we're in SS_MIN mode, we
230961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // always stop.
231061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (single_step_control->method != m) {
231161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
231261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new method";
231361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (single_step_control->step_size == JDWP::SS_MIN) {
231461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
231561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new instruction";
231661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (single_step_control->dex_pcs.find(dex_pc) == single_step_control->dex_pcs.end()) {
231761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
231861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS new line";
231961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
232061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else if (single_step_control->step_depth == JDWP::SD_OVER) {
232161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Step over method calls.  We break when the line number is
232261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // different and the frame depth is <= the original frame
232361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth.  (We can't just compare on the method, because we
232461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // might get unrolled past it by an exception, and it's tricky
232561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // to identify recursion.)
232661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
232761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
232861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
232961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
233061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Popped up one or more frames, always trigger.
233161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
233261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
233361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      } else if (stack_depth == single_step_control->stack_depth) {
233461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        // Same depth, see if we moved.
233561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control->step_size == JDWP::SS_MIN) {
23368696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          event_flags |= kSingleStep;
23378696433d1b3d8ba15288483b777edd888de69135Elliott Hughes          VLOG(jdwp) << "SS new instruction";
233861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        } else if (single_step_control->dex_pcs.find(dex_pc) == single_step_control->dex_pcs.end()) {
23392435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          event_flags |= kSingleStep;
23402435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          VLOG(jdwp) << "SS new line";
234191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes        }
234261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      }
234361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    } else {
234461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      CHECK_EQ(single_step_control->step_depth, JDWP::SD_OUT);
234561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // Return from the current method.  We break when the frame
234661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // depth pops up.
234761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
234861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // This differs from the "method exit" break in that it stops
234961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // with the PC at the next instruction in the returned-to
235061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      // function, rather than the end of the returning function.
235161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
235261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      int stack_depth = GetStackDepth(thread);
235361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (stack_depth < single_step_control->stack_depth) {
235461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        event_flags |= kSingleStep;
235561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        VLOG(jdwp) << "SS method pop";
23568696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
235791bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes    }
235891bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
235991bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
236091bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // If there's something interesting going on, see if it matches one
236191bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  // of the debugger filters.
236291bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  if (event_flags != 0) {
2363579b02416e05e32e535126e1ed61613a2cdb030eJeff Hao    Dbg::PostLocationEvent(m, dex_pc, this_object, event_flags, nullptr);
236491bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes  }
236591bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes}
236691bf6cd47174f5c17265320f7a350722720390a5Elliott Hughes
23678696433d1b3d8ba15288483b777edd888de69135Elliott Hughesvoid Dbg::WatchLocation(const JDWP::JdwpLocation* location) {
236809bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
2369ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(location->method_id);
2370972a47bf0c180dee7e4c697b74dd31daaf58e008Elliott Hughes  gBreakpoints.push_back(Breakpoint(m, location->dex_pc));
23718696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " << gBreakpoints[gBreakpoints.size() - 1];
2372872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2373872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
23748696433d1b3d8ba15288483b777edd888de69135Elliott Hughesvoid Dbg::UnwatchLocation(const JDWP::JdwpLocation* location) {
237509bfc6a50bdc9366b13ac3ab479d9278c853d90ajeffhao  MutexLock mu(Thread::Current(), *Locks::breakpoint_lock_);
2376ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  mirror::ArtMethod* m = FromMethodId(location->method_id);
23778696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  for (size_t i = 0; i < gBreakpoints.size(); ++i) {
2378972a47bf0c180dee7e4c697b74dd31daaf58e008Elliott Hughes    if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == location->dex_pc) {
23798696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
23808696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      gBreakpoints.erase(gBreakpoints.begin() + i);
23818696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      return;
23828696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
23838696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  }
2384872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2385872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2386449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// Scoped utility class to suspend a thread so that we may do tasks such as walk its stack. Doesn't
2387449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao// cause suspension if the thread is the current thread.
2388449db33fafa29578df60e8a323f78d5eb6247e76Jeff Haoclass ScopedThreadSuspension {
2389449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao public:
239033e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers  ScopedThreadSuspension(Thread* self, JDWP::ObjectId thread_id)
239133e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) :
2392449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      thread_(NULL),
2393449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_(JDWP::ERR_NONE),
2394449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      self_suspend_(false),
239533e9566255c426e7a2c8fca5b8a1b6a94a5d352cIan Rogers      other_suspend_(false) {
2396449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    ScopedObjectAccessUnchecked soa(self);
2397449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    {
2398449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2399449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      error_ = DecodeThread(soa, thread_id, thread_);
2400449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
2401449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (error_ == JDWP::ERR_NONE) {
2402449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      if (thread_ == soa.Self()) {
2403449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        self_suspend_ = true;
2404449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      } else {
2405449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        soa.Self()->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
2406449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        jobject thread_peer = gRegistry->GetJObject(thread_id);
2407449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        bool timed_out;
2408d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers        Thread* suspended_thread = ThreadList::SuspendThreadByPeer(thread_peer, true, true,
2409d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers                                                                   &timed_out);
2410449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        CHECK_EQ(soa.Self()->TransitionFromSuspendedToRunnable(), kWaitingForDebuggerSuspension);
2411449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        if (suspended_thread == NULL) {
2412449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          // Thread terminated from under us while suspending.
2413449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          error_ = JDWP::ERR_INVALID_THREAD;
2414449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        } else {
2415449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          CHECK_EQ(suspended_thread, thread_);
2416449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao          other_suspend_ = true;
2417449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao        }
2418449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      }
2419449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
2420449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2421449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2422449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* GetThread() const {
2423449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return thread_;
2424449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2425449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2426449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError GetError() const {
2427449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return error_;
2428449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2429449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2430449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ~ScopedThreadSuspension() {
2431449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    if (other_suspend_) {
2432449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao      Runtime::Current()->GetThreadList()->Resume(thread_, true);
2433449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    }
2434449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  }
2435449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2436449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao private:
2437449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* thread_;
2438449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  JDWP::JdwpError error_;
2439449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool self_suspend_;
2440449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  bool other_suspend_;
2441449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao};
2442449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
2443221229cb523f849f165fdafbf9785010963715daElliott HughesJDWP::JdwpError Dbg::ConfigureStep(JDWP::ObjectId thread_id, JDWP::JdwpStepSize step_size,
244400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                   JDWP::JdwpStepDepth step_depth) {
2445449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  Thread* self = Thread::Current();
2446449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  ScopedThreadSuspension sts(self, thread_id);
2447449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao  if (sts.GetError() != JDWP::ERR_NONE) {
2448449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao    return sts.GetError();
24492435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
24508696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
24512435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
24522435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Work out what Method* we're in, the current line number, and how deep the stack currently
24532435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // is for step-out.
24542435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
24552435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
24560399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  struct SingleStepStackVisitor : public StackVisitor {
245761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    explicit SingleStepStackVisitor(Thread* thread, SingleStepControl* single_step_control,
245861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz                                    int32_t* line_number)
2459b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers        SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
246061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        : StackVisitor(thread, NULL), single_step_control_(single_step_control),
246161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          line_number_(line_number) {
246261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      DCHECK_EQ(single_step_control_, thread->GetSingleStepControl());
246361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->method = NULL;
246461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      single_step_control_->stack_depth = 0;
24658696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
2466ca190666fb11820153f74274c495ba1f913d8a69Ian Rogers
246700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
246800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // annotalysis.
246900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
247061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      mirror::ArtMethod* m = GetMethod();
24710399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      if (!m->IsRuntimeMethod()) {
247261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        ++single_step_control_->stack_depth;
247361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        if (single_step_control_->method == NULL) {
24742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          const mirror::DexCache* dex_cache = m->GetDeclaringClass()->GetDexCache();
247561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->method = m;
247661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          *line_number_ = -1;
24772435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          if (dex_cache != NULL) {
24784445a7e3398a6143939168097a3aa275b734504dIan Rogers            const DexFile& dex_file = *dex_cache->GetDexFile();
247961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz            *line_number_ = dex_file.GetLineNumFromPC(m, GetDexPc());
24802435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          }
24818696433d1b3d8ba15288483b777edd888de69135Elliott Hughes        }
24828696433d1b3d8ba15288483b777edd888de69135Elliott Hughes      }
2483530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return true;
24848696433d1b3d8ba15288483b777edd888de69135Elliott Hughes    }
248561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz
248661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
248761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    int32_t* const line_number_;
24888696433d1b3d8ba15288483b777edd888de69135Elliott Hughes  };
2489449db33fafa29578df60e8a323f78d5eb6247e76Jeff Hao
249061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* const thread = sts.GetThread();
249161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepControl* const single_step_control = thread->GetSingleStepControl();
249261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  DCHECK(single_step_control != nullptr);
249361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  int32_t line_number = -1;
249461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  SingleStepStackVisitor visitor(thread, single_step_control, &line_number);
24950399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
24968696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
24972435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
24982435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Find the dex_pc values that correspond to the current line, for line-based single-stepping.
24992435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
25002435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
25012435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  struct DebugCallbackContext {
250261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    explicit DebugCallbackContext(SingleStepControl* single_step_control, int32_t line_number)
250361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      : single_step_control_(single_step_control), line_number_(line_number),
250461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        last_pc_valid(false), last_pc(0) {
25052435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
25062435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
250761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    static bool Callback(void* raw_context, uint32_t address, uint32_t line_number) {
25082435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      DebugCallbackContext* context = reinterpret_cast<DebugCallbackContext*>(raw_context);
250961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz      if (static_cast<int32_t>(line_number) == context->line_number_) {
25102435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        if (!context->last_pc_valid) {
25112435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          // Everything from this address until the next line change is ours.
25122435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc = address;
25132435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes          context->last_pc_valid = true;
25142435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
25152435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Otherwise, if we're already in a valid range for this line,
25162435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // just keep going (shouldn't really happen)...
25177934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      } else if (context->last_pc_valid) {  // and the line number is new
25182435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        // Add everything from the last entry up until here to the set
25192435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = context->last_pc; dex_pc < address; ++dex_pc) {
252061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          context->single_step_control_->dex_pcs.insert(dex_pc);
25212435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
25222435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        context->last_pc_valid = false;
25232435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
25247934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return false;  // There may be multiple entries for any given line.
25252435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
25262435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
252761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    ~DebugCallbackContext() {
25282435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      // If the line number was the last in the position table...
25292435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      if (last_pc_valid) {
253061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz        size_t end = MethodHelper(single_step_control_->method).GetCodeItem()->insns_size_in_code_units_;
25312435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        for (uint32_t dex_pc = last_pc; dex_pc < end; ++dex_pc) {
253261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz          single_step_control_->dex_pcs.insert(dex_pc);
25332435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes        }
25342435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes      }
25352435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
25362435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
253761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* const single_step_control_;
253861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    const int32_t line_number_;
25392435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    bool last_pc_valid;
25402435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    uint32_t last_pc;
25412435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  };
254261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->dex_pcs.clear();
254361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  const mirror::ArtMethod* m = single_step_control->method;
254461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  if (!m->IsNative()) {
254561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    DebugCallbackContext context(single_step_control, line_number);
25463e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes    MethodHelper mh(m);
25473e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes    mh.GetDexFile().DecodeDebugInfo(mh.GetCodeItem(), m->IsStatic(), m->GetDexMethodIndex(),
25483e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes                                    DebugCallbackContext::Callback, NULL, &context);
25493e2e1a2380e7b2ce402640ea4f6a177c06bd19a2Elliott Hughes  }
25502435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
25512435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
25522435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  // Everything else...
25532435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  //
25542435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
255561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_size = step_size;
255661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->step_depth = step_depth;
255761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  single_step_control->is_active = true;
25588696433d1b3d8ba15288483b777edd888de69135Elliott Hughes
25592435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  if (VLOG_IS_ON(jdwp)) {
256061b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step thread: " << *thread;
256161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step size: " << single_step_control->step_size;
256261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step step depth: " << single_step_control->step_depth;
256361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current method: " << PrettyMethod(single_step_control->method);
256461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current line: " << line_number;
256561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    VLOG(jdwp) << "Single-step current stack depth: " << single_step_control->stack_depth;
25662435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    VLOG(jdwp) << "Single-step dex_pc values:";
256761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    for (std::set<uint32_t>::iterator it = single_step_control->dex_pcs.begin(); it != single_step_control->dex_pcs.end(); ++it) {
2568229feb7a09317919ee51c06d1c3e715cea25da75Elliott Hughes      VLOG(jdwp) << StringPrintf(" %#x", *it);
25692435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes    }
25702435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  }
25712435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes
25722435a5751431152aaeaa2faaa86b2a30d3eecfe3Elliott Hughes  return JDWP::ERR_NONE;
2573872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2574872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
257561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertzvoid Dbg::UnconfigureStep(JDWP::ObjectId thread_id) {
257661b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  ScopedObjectAccessUnchecked soa(Thread::Current());
257761b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
257861b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  Thread* thread;
257961b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  JDWP::JdwpError error = DecodeThread(soa, thread_id, thread);
258087118ed6f3f99e7df33214c277cf200a7b9a7499Sebastien Hertz  if (error == JDWP::ERR_NONE) {
258161b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    SingleStepControl* single_step_control = thread->GetSingleStepControl();
258261b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    DCHECK(single_step_control != nullptr);
258361b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    single_step_control->is_active = false;
258461b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz    single_step_control->dex_pcs.clear();
258561b7f1b05d1fe12d4009316263bf990903e4edffSebastien Hertz  }
2586872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2587872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
258845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughesstatic char JdwpTagToShortyChar(JDWP::JdwpTag tag) {
258945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  switch (tag) {
259045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    default:
259145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      LOG(FATAL) << "unknown JDWP tag: " << PrintableChar(tag);
259245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
259345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Primitives.
259445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BYTE:    return 'B';
259545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CHAR:    return 'C';
259645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_FLOAT:   return 'F';
259745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_DOUBLE:  return 'D';
259845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_INT:     return 'I';
259945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_LONG:    return 'J';
260045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_SHORT:   return 'S';
260145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_VOID:    return 'V';
260245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_BOOLEAN: return 'Z';
260345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
260445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Reference types.
260545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_ARRAY:
260645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_OBJECT:
260745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_STRING:
260845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD:
260945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_THREAD_GROUP:
261045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_LOADER:
261145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    case JDWP::JT_CLASS_OBJECT:
261245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return 'L';
261345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes  }
261445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes}
261545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
261688d630950cb5c6a1cb6457ce03a17c074ae13628Elliott HughesJDWP::JdwpError Dbg::InvokeMethod(JDWP::ObjectId thread_id, JDWP::ObjectId object_id,
261788d630950cb5c6a1cb6457ce03a17c074ae13628Elliott Hughes                                  JDWP::RefTypeId class_id, JDWP::MethodId method_id,
261800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  uint32_t arg_count, uint64_t* arg_values,
261900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* arg_types, uint32_t options,
262000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::JdwpTag* pResultTag, uint64_t* pResultValue,
262100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers                                  JDWP::ObjectId* pExceptionId) {
2622d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  ThreadList* thread_list = Runtime::Current()->GetThreadList();
2623d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2624d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  Thread* targetThread = NULL;
2625d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  DebugInvokeReq* req = NULL;
262600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  Thread* self = Thread::Current();
2627d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
262800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(self);
262950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    MutexLock mu(soa.Self(), *Locks::thread_list_lock_);
2630221229cb523f849f165fdafbf9785010963715daElliott Hughes    JDWP::JdwpError error = DecodeThread(soa, thread_id, targetThread);
2631221229cb523f849f165fdafbf9785010963715daElliott Hughes    if (error != JDWP::ERR_NONE) {
2632221229cb523f849f165fdafbf9785010963715daElliott Hughes      LOG(ERROR) << "InvokeMethod request for invalid thread id " << thread_id;
2633221229cb523f849f165fdafbf9785010963715daElliott Hughes      return error;
2634d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
2635d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    req = targetThread->GetInvokeReq();
2636d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (!req->ready) {
2637d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << "InvokeMethod request for thread not stopped by event: " << *targetThread;
2638d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      return JDWP::ERR_INVALID_THREAD;
2639d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
2640d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2641d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
2642d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We currently have a bug where we don't successfully resume the
2643d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * target thread if the suspend count is too deep.  We're expected to
2644d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * require one "resume" for each "suspend", but when asked to execute
2645d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a method we have to resume fully and then re-suspend it back to the
2646d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * same level.  (The easiest way to cause this is to type "suspend"
2647d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * multiple times in jdb.)
2648d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
2649d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * It's unclear what this means when the event specifies "resume all"
2650d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * and some threads are suspended more deeply than others.  This is
2651d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * a rare problem, so for now we just prevent it from hanging forever
2652d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * by rejecting the method invocation request.  Without this, we will
2653d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * be stuck waiting on a suspended thread.
2654d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
265500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    int suspend_count;
265600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
265750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu2(soa.Self(), *Locks::thread_suspend_count_lock_);
265800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      suspend_count = targetThread->GetSuspendCount();
265900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
2660d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (suspend_count > 1) {
2661d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      LOG(ERROR) << *targetThread << " suspend count too deep for method invocation: " << suspend_count;
26627934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom      return JDWP::ERR_THREAD_SUSPENDED;  // Probably not expected here.
2663d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
2664d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
26653f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    JDWP::JdwpError status;
26662dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* receiver = gRegistry->Get<mirror::Object*>(object_id);
266764f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (receiver == ObjectRegistry::kInvalidObject) {
26683f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
26693f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
267045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
26712dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Object* thread = gRegistry->Get<mirror::Object*>(thread_id);
267264f574f474aa77c72778640ab21f8cfa72546812Elliott Hughes    if (thread == ObjectRegistry::kInvalidObject) {
26733f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return JDWP::ERR_INVALID_OBJECT;
26743f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
267545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // TODO: check that 'thread' is actually a java.lang.Thread!
267645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
26772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = DecodeClass(class_id, status);
267845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (c == NULL) {
26793f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes      return status;
26803f4d58f4a39f60057ebf8ea92340ddb78317ae51Elliott Hughes    }
268145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
2682ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = FromMethodId(method_id);
268345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic() != (receiver == NULL)) {
268445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_INVALID_METHODID;
268545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
268645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (m->IsStatic()) {
268745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (m->GetDeclaringClass() != c) {
268845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
268945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
269045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    } else {
269145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (!m->GetDeclaringClass()->IsAssignableFrom(c)) {
269245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_INVALID_METHODID;
269345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
269445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
269545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
269645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    // Check the argument list matches the method.
269745651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    MethodHelper mh(m);
269845651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (mh.GetShortyLength() - 1 != arg_count) {
269945651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      return JDWP::ERR_ILLEGAL_ARGUMENT;
270045651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
270145651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    const char* shorty = mh.GetShorty();
27020920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes    const DexFile::TypeList* types = mh.GetParameterTypeList();
270345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    for (size_t i = 0; i < arg_count; ++i) {
270445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      if (shorty[i + 1] != JdwpTagToShortyChar(arg_types[i])) {
270545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes        return JDWP::ERR_ILLEGAL_ARGUMENT;
270645651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      }
27070920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
27080920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      if (shorty[i + 1] == 'L') {
27090920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        // Did we really get an argument of an appropriate reference type?
27100920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        mirror::Class* parameter_type = mh.GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
27110920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        mirror::Object* argument = gRegistry->Get<mirror::Object*>(arg_values[i]);
27120920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        if (argument == ObjectRegistry::kInvalidObject) {
27130920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_INVALID_OBJECT;
27140920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
27150920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        if (!argument->InstanceOf(parameter_type)) {
27160920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes          return JDWP::ERR_ILLEGAL_ARGUMENT;
27170920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        }
27180920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes
27190920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        // Turn the on-the-wire ObjectId into a jobject.
27200920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        jvalue& v = reinterpret_cast<jvalue&>(arg_values[i]);
27210920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes        v.l = gRegistry->GetJObject(arg_values[i]);
27220920163b0ee4ce3fbf57db5506659de14b77be75Elliott Hughes      }
272345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
272445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes
2725d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->receiver = receiver;
2726d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->thread = thread;
2727d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->klass = c;
2728d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->method = m;
2729d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_count = arg_count;
2730d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->arg_values = arg_values;
2731d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->options = options;
2732d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    req->invoke_needed = true;
2733d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2734d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2735d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // The fact that we've released the thread list lock is a bit risky --- if the thread goes
2736d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // away we're sitting high and dry -- but we must release this before the ResumeAllThreads
2737d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // call, and it's unwise to hold it during WaitForSuspend.
2738d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2739d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  {
2740d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
2741d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We change our (JDWP thread) status, which should be THREAD_RUNNING,
274281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes     * so we can suspend for a GC if the invoke request causes us to
2743d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * run out of memory.  It's also a good idea to change it before locking
2744d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the invokeReq mutex, although that should never be held for long.
2745d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
274600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSend);
2747d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
27484dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Transferring control to event thread";
2749d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    {
2750d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      MutexLock mu(self, req->lock);
2751d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2752d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
27534dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming all threads";
275400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers        thread_list->UndoDebuggerSuspensions();
2755d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      } else {
27564dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes        VLOG(jdwp) << "      Resuming event thread only";
2757d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes        thread_list->Resume(targetThread, true);
2758d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
2759d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2760d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      // Wait for the request to finish executing.
2761d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz      while (req->invoke_needed) {
2762d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz        req->cond.Wait(self);
2763d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      }
2764d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
27654dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "    Control has returned from event thread";
2766d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2767d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* wait for thread to re-suspend itself */
2768df62950e7a32031b82360c407d46a37b94188fbbBrian Carlstrom    SuspendThread(thread_id, false /* request_suspension */);
276900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
2770d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2771d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2772d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  /*
2773d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * Suspend the threads.  We waited for the target thread to suspend
2774d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * itself, so all we need to do is suspend the others.
2775d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   *
2776d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * The suspendAllThreads() call will double-suspend the event thread,
2777d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   * so we want to resume the target thread once to keep the books straight.
2778d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes   */
2779d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if ((options & JDWP::INVOKE_SINGLE_THREADED) == 0) {
278000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromRunnableToSuspended(kWaitingForDebuggerSuspension);
27814dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Suspending all threads";
278200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    thread_list->SuspendAllForDebugger();
278300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    self->TransitionFromSuspendedToRunnable();
27844dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "      Resuming event thread to balance the count";
2785d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    thread_list->Resume(targetThread, true);
2786d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2787d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2788d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Copy the result.
2789d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pResultTag = req->result_tag;
2790d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (IsPrimitiveTag(req->result_tag)) {
2791f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = req->result_value.GetJ();
2792d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else {
2793f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    *pResultValue = gRegistry->Add(req->result_value.GetL());
2794d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2795d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  *pExceptionId = req->exception;
2796d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  return req->error;
2797872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2798872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2799872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::ExecuteMethod(DebugInvokeReq* pReq) {
280000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
2801d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
280281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes  // We can be called while an exception is pending. We need
2803d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // to preserve that across the method invocation.
280462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SirtRef<mirror::Object> old_throw_this_object(soa.Self(), NULL);
2805ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom  SirtRef<mirror::ArtMethod> old_throw_method(soa.Self(), NULL);
280662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  SirtRef<mirror::Throwable> old_exception(soa.Self(), NULL);
280762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  uint32_t old_throw_dex_pc;
280862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  {
280962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation old_throw_location;
281062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    mirror::Throwable* old_exception_obj = soa.Self()->GetException(&old_throw_location);
281162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_this_object.reset(old_throw_location.GetThis());
281262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_method.reset(old_throw_location.GetMethod());
281362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_exception.reset(old_exception_obj);
281462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    old_throw_dex_pc = old_throw_location.GetDexPc();
281562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->ClearException();
281662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  }
2817d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2818d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  // Translate the method through the vtable, unless the debugger wants to suppress it.
2819d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz  mirror::ArtMethod* m = pReq->method;
2820d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz  if ((pReq->options & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver != NULL) {
2821d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz    mirror::ArtMethod* actual_method = pReq->klass->FindVirtualMethodForVirtualOrInterface(pReq->method);
282245651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    if (actual_method != m) {
282345651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) << " to " << PrettyMethod(actual_method);
282445651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes      m = actual_method;
282545651fde99f52546e71241bb0e8a10d16a9f216aElliott Hughes    }
2826d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2827cfa9cfa3a94b33d300e1b08b48b3cb72de343dbdElliott Hughes  VLOG(jdwp) << "ExecuteMethod " << PrettyMethod(m)
2828d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " receiver=" << pReq->receiver
2829d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz             << " arg_count=" << pReq->arg_count;
2830d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  CHECK(m != NULL);
2831d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2832d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  CHECK_EQ(sizeof(jvalue), sizeof(uint64_t));
2833d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
28345d9173014c1ca09f7249a6b07629aa37778b5f8fJeff Hao  MethodHelper mh(m);
28355d9173014c1ca09f7249a6b07629aa37778b5f8fJeff Hao  ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
2836d38667a055d507492fd05f78519a7e1f0b85ea03Sebastien Hertz  arg_array.BuildArgArray(soa, pReq->receiver, reinterpret_cast<jvalue*>(pReq->arg_values));
28376474d190d5604898354ebf767f1944b6e3e9b445Jeff Hao  InvokeWithArgArray(soa, m, &arg_array, &pReq->result_value, mh.GetShorty()[0]);
2838d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
283962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  mirror::Throwable* exception = soa.Self()->GetException(NULL);
284062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  soa.Self()->ClearException();
284162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  pReq->exception = gRegistry->Add(exception);
2842d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  pReq->result_tag = BasicTagFromDescriptor(MethodHelper(m).GetShorty());
2843d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (pReq->exception != 0) {
284462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    VLOG(jdwp) << "  JDWP invocation returning with exception=" << exception
284562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        << " " << exception->Dump();
2846f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    pReq->result_value.SetJ(0);
2847d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  } else if (pReq->result_tag == JDWP::JT_OBJECT) {
2848d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /* if no exception thrown, examine object result more closely */
2849f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    JDWP::JdwpTag new_tag = TagFromObject(pReq->result_value.GetL());
2850d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    if (new_tag != pReq->result_tag) {
28514dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes      VLOG(jdwp) << "  JDWP promoted result from " << pReq->result_tag << " to " << new_tag;
2852d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes      pReq->result_tag = new_tag;
2853d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    }
2854d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2855d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes    /*
2856d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * Register the object.  We don't actually need an ObjectId yet,
2857d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * but we do need to be sure that the GC won't move or discard the
2858d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * object when we switch out of RUNNING.  The ObjectId conversion
2859d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * will add the object to the "do not touch" list.
2860d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     *
2861d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * We can't use the "tracked allocation" mechanism here because
2862d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     * the object is going to be handed off to a different thread.
2863d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes     */
2864f24d3cedd395690f6904aaac80f84a100420f7a3Elliott Hughes    gRegistry->Add(pReq->result_value.GetL());
2865d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2866d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes
2867d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  if (old_exception.get() != NULL) {
286862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
286962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                         old_throw_dex_pc);
287062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    soa.Self()->SetException(gc_safe_throw_location, old_exception.get());
2871d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes  }
2872872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2873872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2874d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes/*
28754b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes * "request" contains a full JDWP packet, possibly with multiple chunks.  We
2876f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * need to process each, accumulate the replies, and ship the whole thing
2877f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * back.
2878f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
2879f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * Returns "true" if we have a reply.  The reply buffer is newly allocated,
2880f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * and includes the chunk type/length, followed by the data.
2881f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes *
28823d30d9b09c16542d41554aad9f46ec9109ba5cb5Elliott Hughes * OLD-TODO: we currently assume that the request and reply include a single
2883f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes * chunk.  If this becomes inconvenient we will need to adapt.
2884f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes */
28854b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughesbool Dbg::DdmHandlePacket(JDWP::Request& request, uint8_t** pReplyBuf, int* pReplyLen) {
2886f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  Thread* self = Thread::Current();
2887f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  JNIEnv* env = self->GetJniEnv();
2888f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
28894b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t type = request.ReadUnsigned32("type");
28904b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  uint32_t length = request.ReadUnsigned32("length");
28914b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes
28924b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  // Create a byte[] corresponding to 'request'.
28934b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  size_t request_length = request.size();
28944b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(request_length));
28956a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (dataArray.get() == NULL) {
28964b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    LOG(WARNING) << "byte[] allocation failed: " << request_length;
2897f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
2898f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
2899f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
29004b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  env->SetByteArrayRegion(dataArray.get(), 0, request_length, reinterpret_cast<const jbyte*>(request.data()));
29014b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  request.Skip(request_length);
2902f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
2903f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Run through and find all chunks.  [Currently just find the first.]
29046a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ScopedByteArrayRO contents(env, dataArray.get());
29054b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  if (length != request_length) {
29064b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes    LOG(WARNING) << StringPrintf("bad chunk found (len=%u pktLen=%d)", length, request_length);
2907f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
2908f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
2909f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
2910f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
2911eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jobject> chunk(env, env->CallStaticObjectMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
2912eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                                                                 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
29134b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes                                                                 type, dataArray.get(), 0, length));
2914f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (env->ExceptionCheck()) {
2915f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type);
2916f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionDescribe();
2917f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    env->ExceptionClear();
2918f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
2919f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
2920f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
29216a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (chunk.get() == NULL) {
2922f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
2923f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
2924f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
2925f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  /*
2926f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * Pull the pieces out of the chunk.  We copy the results into a
2927f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * newly-allocated buffer that the caller can free.  We don't want to
2928f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * continue using the Chunk object because nothing has a reference to it.
2929f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
2930f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * We could avoid this by returning type/data/offset/length and having
2931f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * the caller be aware of the object lifetime issues, but that
293281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes   * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
2933f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * if we have responses for multiple chunks.
2934f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   *
2935f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   * So we're pretty much stuck with copying data around multiple times.
2936f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes   */
2937eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  ScopedLocalRef<jbyteArray> replyData(env, reinterpret_cast<jbyteArray>(env->GetObjectField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
29384b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  jint offset = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
2939eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  length = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
2940eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  type = env->GetIntField(chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
2941f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
29424dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d", type, replyData.get(), offset, length);
29436a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (length == 0 || replyData.get() == NULL) {
2944f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
2945f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
2946f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
29474b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  const int kChunkHdrLen = 8;
2948f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  uint8_t* reply = new uint8_t[length + kChunkHdrLen];
2949f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  if (reply == NULL) {
2950f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    LOG(WARNING) << "malloc failed: " << (length + kChunkHdrLen);
2951f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes    return false;
2952f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  }
2953f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 0, type);
2954f7c3b6625d710a8700325eea447f65e9f963b7f2Elliott Hughes  JDWP::Set4BE(reply + 4, length);
29556a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  env->GetByteArrayRegion(replyData.get(), offset, length, reinterpret_cast<jbyte*>(reply + kChunkHdrLen));
2956f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
2957f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyBuf = reply;
2958f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  *pReplyLen = length + kChunkHdrLen;
2959f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes
29604b9702c6912c6f8745a77f5b5af56e7fe196e7c2Elliott Hughes  VLOG(jdwp) << StringPrintf("dvmHandleDdm returning type=%.4s %p len=%d", reinterpret_cast<char*>(reply), reply, length);
2961f6a1e1e44b94221c4e6b063b36921c712a33910bElliott Hughes  return true;
2962872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2963872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2964a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::DdmBroadcast(bool connect) {
29654dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes  VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
296647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
296747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  Thread* self = Thread::Current();
296850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  if (self->GetState() != kRunnable) {
296950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
297050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    /* try anyway? */
297147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
297247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
297347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  JNIEnv* env = self->GetJniEnv();
297447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
2975eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes  env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
2976eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
2977eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes                            event);
297847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (env->ExceptionCheck()) {
297947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
298047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionDescribe();
298147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    env->ExceptionClear();
298247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
298347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
298447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
2985872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmConnected() {
2986a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(true);
2987872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
2988872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
2989872d4ec7225444d9400d30f9027247deb91012fdElliott Hughesvoid Dbg::DdmDisconnected() {
2990a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::DdmBroadcast(false);
299147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = false;
299247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
299347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
299447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes/*
29958218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes * Send a notification when a thread starts, stops, or changes its name.
299647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes *
299747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * Because we broadcast the full set of threads when the notifications are
299847fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes * first enabled, it's possible for "thread" to be actively executing.
299947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes */
30008218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
300147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (!gDdmThreadNotification) {
300247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    return;
300347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
300447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
30058218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  if (type == CHUNK_TYPE("THDE")) {
30068218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    uint8_t buf[4];
3007d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Set4BE(&buf[0], t->GetThreadId());
30088218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    Dbg::DdmSendChunk(CHUNK_TYPE("THDE"), 4, buf);
30098218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  } else {
30108218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
301100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ScopedObjectAccessUnchecked soa(Thread::Current());
30122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    SirtRef<mirror::String> name(soa.Self(), t->GetThreadName(soa));
301347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes    size_t char_count = (name.get() != NULL) ? name->GetLength() : 0;
3014725a957985171d712d5c048cc3d00ff14968784bjeffhao    const jchar* chars = (name.get() != NULL) ? name->GetCharArray()->GetData() : NULL;
301547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
301621f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    std::vector<uint8_t> bytes;
3017d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers    JDWP::Append4BE(bytes, t->GetThreadId());
3018545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    JDWP::AppendUtf16BE(bytes, chars, char_count);
301921f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
302021f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes    Dbg::DdmSendChunk(type, bytes);
302147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
302247fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
302347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
302447fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::DdmSetThreadNotification(bool enable) {
302500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // Enable/disable thread notifications.
302647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  gDdmThreadNotification = enable;
302747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  if (enable) {
302800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // Suspend the VM then post thread start notifications for all threads. Threads attaching will
302900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // see a suspension in progress and block until that ends. They then post their own start
303000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    // notification.
303100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    SuspendVM();
303200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    std::list<Thread*> threads;
303350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers    Thread* self = Thread::Current();
303400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
303550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      MutexLock mu(self, *Locks::thread_list_lock_);
303600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      threads = Runtime::Current()->GetThreadList()->GetList();
303700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
303800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    {
303950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      ScopedObjectAccess soa(self);
304002e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      for (Thread* thread : threads) {
304102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier        Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR"));
304200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers      }
304300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
304400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    ResumeVM();
304547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
304647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
304747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
3048a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughesvoid Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
3049c0f0933249cf516b37717faa766e1e9808f7c1f8Elliott Hughes  if (IsDebuggerActive()) {
3050dbe6f4613ae0161b169f4fca8a616b0b393370abMathieu Chartier    ScopedObjectAccessUnchecked soa(Thread::Current());
3051cfaa455374aae0a08c8cb28b5bb306b17866d652Ian Rogers    JDWP::ObjectId id = gRegistry->Add(t->GetPeer());
30528218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes    gJdwpState->PostThreadChange(id, type == CHUNK_TYPE("THCR"));
305347fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes  }
30548218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughes  Dbg::DdmSendThreadNotification(t, type);
305547fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
305647fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
305747fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadStart(Thread* t) {
3058a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
305947fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes}
306047fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughes
306147fce01c0f27dba716fa6b97242562fbc5c26eeaElliott Hughesvoid Dbg::PostThreadDeath(Thread* t) {
3062a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
3063872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3064872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
30658218847294600bbfcdc041a46c2b579b6e70cf3bElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, size_t byte_count, const uint8_t* buf) {
30663bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  CHECK(buf != NULL);
30673bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  iovec vec[1];
30683bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_base = reinterpret_cast<void*>(const_cast<uint8_t*>(buf));
30693bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  vec[0].iov_len = byte_count;
30703bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  Dbg::DdmSendChunkV(type, vec, 1);
3071872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3072872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
307321f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughesvoid Dbg::DdmSendChunk(uint32_t type, const std::vector<uint8_t>& bytes) {
307421f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  DdmSendChunk(type, bytes.size(), &bytes[0]);
307521f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes}
307621f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes
3077f52935278fca8c7aa220543eef4544e3d1105d91Brian Carlstromvoid Dbg::DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count) {
30783bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  if (gJdwpState == NULL) {
30794dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes    VLOG(jdwp) << "Debugger thread not active, ignoring DDM send: " << type;
30803bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  } else {
3081cccd84f1f972f1a260c3be418c8388a5d30cf59eElliott Hughes    gJdwpState->DdmSendChunkV(type, iov, iov_count);
30823bb81563481d02b5a6349b8ed918392454e761d8Elliott Hughes  }
3083872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}
3084872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes
3085767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesint Dbg::DdmHandleHpifChunk(HpifWhen when) {
3086767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when == HPIF_WHEN_NOW) {
30877162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    DdmSendHeapInfo(when);
3088767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return true;
3089767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3090767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3091767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
3092767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
3093767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3094767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3095767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3096767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  gDdmHpifWhen = when;
3097767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
3098767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3099767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3100767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughesbool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
3101767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
3102767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
3103767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3104767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3105767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3106767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
3107767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
3108767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    return false;
3109767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3110767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3111767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  if (native) {
3112767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhen = when;
3113767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmNhsgWhat = what;
3114767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  } else {
3115767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhen = when;
3116767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes    gDdmHpsgWhat = what;
3117767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  }
3118767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes  return true;
3119767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3120767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
31217162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughesvoid Dbg::DdmSendHeapInfo(HpifWhen reason) {
31227162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  // If there's a one-shot 'when', reset it.
31237162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  if (reason == gDdmHpifWhen) {
31247162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
31257162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes      gDdmHpifWhen = HPIF_WHEN_NEVER;
31267162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes    }
31277162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  }
31287162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes
31297162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  /*
31307162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Chunk HPIF (client --> server)
31317162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
31327162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * Heap Info. General information about the heap,
31337162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   * suitable for a summary display.
31347162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
31357162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   [u4]: number of heaps
31367162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *
31377162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *   For each heap:
31387162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: heap ID
31397162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u8]: timestamp in ms since Unix epoch
31407162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u1]: capture reason (same as 'when' value from server)
31417162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: max heap size in bytes (-Xmx)
31427162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current heap size in bytes
31437162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of bytes allocated
31447162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   *     [u4]: current number of objects allocated
31457162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes   */
31467162ad937f5f6bec32bf78d4675ff65cd6d1a233Elliott Hughes  uint8_t heap_count = 1;
31471d54e73444e017d3a65234e0f193846f3e27472bIan Rogers  gc::Heap* heap = Runtime::Current()->GetHeap();
314821f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  std::vector<uint8_t> bytes;
3149545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append4BE(bytes, heap_count);
31507934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, 1);  // Heap id (bogus; we only have one heap).
3151545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append8BE(bytes, MilliTime());
3152545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  JDWP::Append1BE(bytes, reason);
31537934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetMaxMemory());  // Max allowed heap size in bytes.
31547934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Append4BE(bytes, heap->GetTotalMemory());  // Current heap size in bytes.
3155b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetBytesAllocated());
3156b3bd5f07884f5a1f2b84224363b1372d7c28d447Elliott Hughes  JDWP::Append4BE(bytes, heap->GetObjectsAllocated());
315721f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
315821f32d704a21bcd67d7b87b149b6314ff92946cbElliott Hughes  Dbg::DdmSendChunk(CHUNK_TYPE("HPIF"), bytes);
3159767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3160767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
31616a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgSolidity {
31626a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FREE = 0,
31636a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_HARD = 1,
31646a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SOFT = 2,
31656a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_WEAK = 3,
31666a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_PHANTOM = 4,
31676a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_FINALIZABLE = 5,
31686a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  SOLIDITY_SWEEP = 6,
31696a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
31706a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
31716a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesenum HpsgKind {
31726a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_OBJECT = 0,
31736a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_CLASS_OBJECT = 1,
31746a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_1 = 2,
31756a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_2 = 3,
31766a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_4 = 4,
31776a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_ARRAY_8 = 5,
31786a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_UNKNOWN = 6,
31796a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  KIND_NATIVE = 7,
31806a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes};
31816a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
31826a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_PARTIAL (1<<7)
31836a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
31846a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
318530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogersclass HeapChunkContext {
318630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers public:
31876a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Maximum chunk size.  Obtain this from the formula:
31886a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
31896a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  HeapChunkContext(bool merge, bool native)
319030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      : buf_(16384 - 16),
319130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        type_(0),
319230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers        merge_(merge) {
31936a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
31946a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    if (native) {
319530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = CHUNK_TYPE("NHSG");
31966a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    } else {
319730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
31986a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
31996a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
32006a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
32016a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  ~HeapChunkContext() {
320230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (p_ > &buf_[0]) {
32036a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      Flush();
32046a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
32056a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
32066a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
32076a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void EnsureHeader(const void* chunk_ptr) {
320830fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    if (!needHeader_) {
32096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes      return;
32106a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
32116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
32126a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Start a new HPSx chunk.
32137934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 1);  // Heap id (bogus; we only have one heap).
32147934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write1BE(&p_, 8);  // Size of allocation unit, in bytes.
32156a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
32167934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr));  // virtual address of segment start.
32177934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Write4BE(&p_, 0);  // offset of this piece (relative to the virtual address).
32186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // [u4]: length of piece, in allocation units
32196a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // We won't know this until we're done, so save the offset and stuff in a dummy value.
322030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = p_;
322130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Write4BE(&p_, 0x55555555);
322230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = false;
32236a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
32246a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3225b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers  void Flush() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3226d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    if (pieceLenField_ == NULL) {
3227d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
3228d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      CHECK(needHeader_);
3229d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers      return;
3230d636b0623307a379e255a9aaa682c12a2acc3a92Ian Rogers    }
32316a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    // Patch the "length of piece" field.
323230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(&buf_[0], pieceLenField_);
323330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    CHECK_LE(pieceLenField_, p_);
323430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    JDWP::Set4BE(pieceLenField_, totalAllocationUnits_);
32356a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
323630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    Dbg::DdmSendChunk(type_, p_ - &buf_[0], &buf_[0]);
32376a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    Reset();
32386a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
32396a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
324000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  static void HeapChunkCallback(void* start, void* end, size_t used_bytes, void* arg)
3241b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3242b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
324330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkCallback(start, end, used_bytes);
3244a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  }
3245a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
32466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes private:
3247a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  enum { ALLOCATION_UNIT_SIZE = 8 };
3248a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
32496a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  void Reset() {
325030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    p_ = &buf_[0];
325115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    startOfNextMemoryChunk_ = NULL;
325230fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    totalAllocationUnits_ = 0;
325330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    needHeader_ = true;
325430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    pieceLenField_ = NULL;
32556a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
32566a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
325700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  void HeapChunkCallback(void* start, void* /*end*/, size_t used_bytes)
3258b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_,
3259b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers                            Locks::mutator_lock_) {
326030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
326130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    // in the following code not to allocate memory, by ensuring buf_ is of the correct size
326215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (used_bytes == 0) {
326315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start == NULL) {
326415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            // Reset for start of new heap.
326515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
326615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
326715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
326815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Only process in use memory so that free region information
326915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // also includes dlmalloc book keeping.
3270a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return;
3271a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
32726a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
327315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    /* If we're looking at the native heap, we'll just return
327415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     * (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks
327515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers     */
327615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bool native = type_ == CHUNK_TYPE("NHSG");
327715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
327815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (startOfNextMemoryChunk_ != NULL) {
327915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // Transmit any pending free memory. Native free memory of
328015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // over kMaxFreeLen could be because of the use of mmaps, so
328115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        // don't report. If not free memory then start a new segment.
328215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        bool flush = true;
328315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (start > startOfNextMemoryChunk_) {
328415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            const size_t kMaxFreeLen = 2 * kPageSize;
328515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeStart = startOfNextMemoryChunk_;
328615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            void* freeEnd = start;
32872d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom            size_t freeLen = reinterpret_cast<char*>(freeEnd) - reinterpret_cast<char*>(freeStart);
328815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            if (!native || freeLen < kMaxFreeLen) {
328915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), freeStart, freeLen);
329015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers                flush = false;
329115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            }
329215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
329315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        if (flush) {
329415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            startOfNextMemoryChunk_ = NULL;
329515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers            Flush();
329615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers        }
329715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
32982dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    const mirror::Object* obj = reinterpret_cast<const mirror::Object*>(start);
3299a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
3300a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Determine the type of this chunk.
3301a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
3302a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If it's the same, we should combine them.
330315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    uint8_t state = ExamineObject(obj, native);
330415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
330515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // allocation then the first sizeof(size_t) may belong to it.
330615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    const size_t dlMallocOverhead = sizeof(size_t);
330715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    AppendChunk(state, start, used_bytes + dlMallocOverhead);
33082d88862f0752a7a0e65145b088f49dabd49d4284Brian Carlstrom    startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + dlMallocOverhead;
330915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  }
331015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers
331115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void AppendChunk(uint8_t state, void* ptr, size_t length)
3312b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
331315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // Make sure there's enough room left in the buffer.
331415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
331515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    // 17 bytes for any header.
331615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t needed = (((length/ALLOCATION_UNIT_SIZE + 255) / 256) * 2) + 17;
331715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    size_t bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
331815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
331915bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      Flush();
332015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
3321a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes
332215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    bytesLeft = buf_.size() - (size_t)(p_ - &buf_[0]);
332315bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    if (bytesLeft < needed) {
332415bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
332515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers          << needed << " bytes)";
332615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return;
332715bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    }
332815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    EnsureHeader(ptr);
3329a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // Write out the chunk description.
333015bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    length /= ALLOCATION_UNIT_SIZE;   // Convert to allocation units.
333115bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    totalAllocationUnits_ += length;
333215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    while (length > 256) {
333330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = state | HPSG_PARTIAL;
333430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers      *p_++ = 255;     // length - 1
333515bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      length -= 256;
3336a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
333730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers    *p_++ = state;
333815bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers    *p_++ = length - 1;
33396a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
33406a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
33412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  uint8_t ExamineObject(const mirror::Object* o, bool is_native_heap)
3342b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
3343a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (o == NULL) {
3344a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_FREE, 0);
3345a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
33466a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3347a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // It's an allocated chunk. Figure out what it is.
33486a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3349a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // If we're looking at the native heap, we'll just return
3350a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    // (SOLIDITY_HARD, KIND_NATIVE) for all allocated chunks.
335100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    if (is_native_heap) {
3352a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
33536a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
33546a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
33555bfa60ffcc953340feb711ed05cf576ac821905eIan Rogers    if (!Runtime::Current()->GetHeap()->IsLiveObjectLocked(o)) {
335615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
335700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers    }
335800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers
33592dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* c = o->GetClass();
3360a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c == NULL) {
3361a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      // The object was probably just created but hasn't been initialized yet.
3362a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
3363a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
33646a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3365590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier    if (!Runtime::Current()->GetHeap()->IsValidObjectAddress(c)) {
336615bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers      LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
3367a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
3368a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    }
33696a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3370a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsClassClass()) {
3371a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
33726a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
33736a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3374a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    if (c->IsArrayClass()) {
3375a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      if (o->IsObjectArray()) {
3376a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes        return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3377a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
3378a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      switch (c->GetComponentSize()) {
3379a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
3380a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
3381a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
3382a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
3383a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes      }
33846a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    }
33856a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3386a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes    return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
33876a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
33886a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
338930fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  std::vector<uint8_t> buf_;
339030fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* p_;
339130fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint8_t* pieceLenField_;
339215bf2d34efa2218e287b584fb3653d268b9edc8dIan Rogers  void* startOfNextMemoryChunk_;
339330fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  size_t totalAllocationUnits_;
339430fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  uint32_t type_;
339530fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool merge_;
339630fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers  bool needHeader_;
339730fab40ee5a07af6b8c3b6b0e9438071695a57f4Ian Rogers
3398a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
3399a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes};
34006a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
34016a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughesvoid Dbg::DdmSendHeapSegments(bool native) {
34026a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhen when;
34036a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::HpsgWhat what;
34046a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (!native) {
34056a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmHpsgWhen;
34066a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmHpsgWhat;
34076a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  } else {
34086a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    when = gDdmNhsgWhen;
34096a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    what = gDdmNhsgWhat;
34106a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
34116a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  if (when == HPSG_WHEN_NEVER) {
34126a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes    return;
34136a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  }
34146a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
34156a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Figure out what kind of chunks we'll be sending.
34166a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS) << static_cast<int>(what);
34176a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
34186a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // First, send a heap start chunk.
34196a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  uint8_t heap_id[4];
34207934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom  JDWP::Set4BE(&heap_id[0], 1);  // Heap id (bogus; we only have one heap).
34216a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"), sizeof(heap_id), heap_id);
34226a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes
3423cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Thread* self = Thread::Current();
3424cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
3425cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // To allow the Walk/InspectAll() below to exclusively-lock the
3426cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // mutator lock, temporarily release the shared access to the
3427cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // mutator lock here by transitioning to the suspended state.
3428cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Locks::mutator_lock_->AssertSharedHeld(self);
3429cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  self->TransitionFromRunnableToSuspended(kSuspended);
3430cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
34316a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Send a series of heap segment chunks.
3432a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  HeapChunkContext context((what == HPSG_WHAT_MERGED_OBJECTS), native);
3433a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  if (native) {
34341d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    dlmalloc_inspect_all(HeapChunkContext::HeapChunkCallback, &context);
3435a215526d5c789cbef0f81a1f9aba22541a841ccaElliott Hughes  } else {
34361d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    gc::Heap* heap = Runtime::Current()->GetHeap();
34371d54e73444e017d3a65234e0f193846f3e27472bIan Rogers    const std::vector<gc::space::ContinuousSpace*>& spaces = heap->GetContinuousSpaces();
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) {
3441cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi      if ((*cur)->IsMallocSpace()) {
3442cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi        (*cur)->AsMallocSpace()->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
3449cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  // Shared-lock the mutator lock back.
3450cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  self->TransitionFromSuspendedToRunnable();
3451cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi  Locks::mutator_lock_->AssertSharedHeld(self);
3452cf58d4adf461eb9b8e84baa8019054c88cd8acc6Hiroshi Yamauchi
34536a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  // Finally, send a heap end chunk.
34546a5bd495ff2f614f1495f652c86f3902d3bde537Elliott Hughes  Dbg::DdmSendChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"), sizeof(heap_id), heap_id);
3455767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes}
3456767a147529da3ee8240f3ce4cd3af22ae454be64Elliott Hughes
3457b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughesstatic size_t GetAllocTrackerMax() {
3458b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#ifdef HAVE_ANDROID_OS
3459b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  // Check whether there's a system property overriding the number of records.
3460b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  const char* propertyName = "dalvik.vm.allocTrackerMax";
3461b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  char allocRecordMaxString[PROPERTY_VALUE_MAX];
3462b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  if (property_get(propertyName, allocRecordMaxString, "") > 0) {
3463b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    char* end;
3464b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    size_t value = strtoul(allocRecordMaxString, &end, 10);
3465b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (*end != '\0') {
34663e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
34673e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- invalid";
3468b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
3469b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
3470b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    if (!IsPowerOfTwo(value)) {
34713e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk      LOG(ERROR) << "Ignoring  " << propertyName << " '" << allocRecordMaxString
34723e47a748eb646b8d2fc8e8c4f11b270d9ae2c607Ruben Brunk                 << "' --- not power of two";
3473b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      return kDefaultNumAllocRecords;
3474b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    }
3475b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    return value;
3476b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  }
3477b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes#endif
3478b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  return kDefaultNumAllocRecords;
3479b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes}
3480b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes
3481545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::SetAllocTrackingEnabled(bool enabled) {
348250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(Thread::Current(), gAllocTrackerLock);
3483545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (enabled) {
3484545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if (recent_allocation_records_ == NULL) {
3485b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      gAllocRecordMax = GetAllocTrackerMax();
3486b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      LOG(INFO) << "Enabling alloc tracker (" << gAllocRecordMax << " entries of "
3487b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes                << kMaxAllocRecordStackDepth << " frames, taking "
3488b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes                << PrettySize(sizeof(AllocRecord) * gAllocRecordMax) << ")";
3489545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      gAllocRecordHead = gAllocRecordCount = 0;
3490b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      recent_allocation_records_ = new AllocRecord[gAllocRecordMax];
3491545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      CHECK(recent_allocation_records_ != NULL);
3492545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3493fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers    Runtime::Current()->GetInstrumentation()->InstrumentQuickAllocEntryPoints();
3494545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  } else {
3495fa82427c68b09f4aedbee319dc71579afbfc66f5Ian Rogers    Runtime::Current()->GetInstrumentation()->UninstrumentQuickAllocEntryPoints();
3496545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    delete[] recent_allocation_records_;
3497545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    recent_allocation_records_ = NULL;
3498545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3499545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
3500545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
35010399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogersstruct AllocRecordStackVisitor : public StackVisitor {
35027a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor(Thread* thread, AllocRecord* record)
3503b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
35047a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers      : StackVisitor(thread, NULL), record(record), depth(0) {}
3505545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
350600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // TODO: Enable annotalysis. We know lock is held in constructor, but abstraction confuses
350700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  // annotalysis.
350800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
3509545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if (depth >= kMaxAllocRecordStackDepth) {
3510530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      return false;
3511545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3512ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom    mirror::ArtMethod* m = GetMethod();
35130399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers    if (!m->IsRuntimeMethod()) {
35140399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].method = m;
35150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].dex_pc = GetDexPc();
3516530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes      ++depth;
3517545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3518530fa005e2944d3b12712f80d974f0e753f568efElliott Hughes    return true;
3519545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3520545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3521545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  ~AllocRecordStackVisitor() {
3522545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // Clear out any unused stack trace elements.
3523545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (; depth < kMaxAllocRecordStackDepth; ++depth) {
3524545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      record->stack[depth].method = NULL;
35250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers      record->stack[depth].dex_pc = 0;
3526545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3527545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3528545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3529545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  AllocRecord* record;
3530545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t depth;
3531545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
3532545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
35332dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Dbg::RecordAllocation(mirror::Class* type, size_t byte_count) {
3534545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  Thread* self = Thread::Current();
3535545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  CHECK(self != NULL);
3536545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
353750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(self, gAllocTrackerLock);
3538545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
3539545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
3540545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3541545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3542545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Advance and clip.
3543b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  if (++gAllocRecordHead == gAllocRecordMax) {
3544545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    gAllocRecordHead = 0;
3545545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3546545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3547545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the basics.
3548545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  AllocRecord* record = &recent_allocation_records_[gAllocRecordHead];
3549545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  record->type = type;
3550545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  record->byte_count = byte_count;
3551d9c4fc94fa618617f94e1de9af5f034549100753Ian Rogers  record->thin_lock_id = self->GetThreadId();
3552545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3553545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // Fill in the stack trace.
35547a22fa657b972e8323692368975bc5a7be1cc0f5Ian Rogers  AllocRecordStackVisitor visitor(self, record);
35550399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers  visitor.WalkStack();
3556545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3557b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  if (gAllocRecordCount < gAllocRecordMax) {
3558545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    ++gAllocRecordCount;
3559545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3560545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
3561545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3562a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// Returns the index of the head element.
3563a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
3564a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We point at the most-recently-written record, so if gAllocRecordCount is 1
3565a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// we want to use the current element.  Take "head+1" and subtract count
3566a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// from it.
3567a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes//
3568a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes// We need to handle underflow in our circular buffer, so we add
3569b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes// gAllocRecordMax and then mask it back down.
3570f8349361a16a4e2796efe9f3586b994e8d4834e4Elliott Hughesstatic inline int HeadIndex() EXCLUSIVE_LOCKS_REQUIRED(gAllocTrackerLock) {
3571b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes  return (gAllocRecordHead+1 + gAllocRecordMax - gAllocRecordCount) & (gAllocRecordMax-1);
3572545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
3573545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3574545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesvoid Dbg::DumpRecentAllocations() {
357500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers  ScopedObjectAccess soa(Thread::Current());
357650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  MutexLock mu(soa.Self(), gAllocTrackerLock);
3577545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (recent_allocation_records_ == NULL) {
3578545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    LOG(INFO) << "Not recording tracked allocations";
3579545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return;
3580545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3581545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3582545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // "i" is the head of the list.  We want to start at the end of the
3583545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  // list and move forward to the tail.
3584a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t i = HeadIndex();
3585545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  size_t count = gAllocRecordCount;
3586545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3587545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  LOG(INFO) << "Tracked allocations, (head=" << gAllocRecordHead << " count=" << count << ")";
3588545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  while (count--) {
3589545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    AllocRecord* record = &recent_allocation_records_[i];
3590545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3591a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    LOG(INFO) << StringPrintf(" Thread %-2d %6zd bytes ", record->thin_lock_id, record->byte_count)
3592545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes              << PrettyClass(record->type);
3593545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3594545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
3595ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom      const mirror::ArtMethod* m = record->stack[stack_frame].method;
3596545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      if (m == NULL) {
3597545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes        break;
3598545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
3599545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      LOG(INFO) << "    " << PrettyMethod(m) << " line " << record->stack[stack_frame].LineNumber();
3600545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3601545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3602545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    // pause periodically to help logcat catch up
3603545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    if ((count % 5) == 0) {
3604545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      usleep(40000);
3605545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3606545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3607b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes    i = (i + 1) & (gAllocRecordMax-1);
3608545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3609545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
3610545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3611545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughesclass StringTable {
3612545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes public:
3613545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  StringTable() {
3614545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3615545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
36166d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers  void Add(const char* s) {
3617545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    table_.insert(s);
3618545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3619545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3620a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t IndexOf(const char* s) const {
362102e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    auto it = table_.find(s);
3622a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    if (it == table_.end()) {
3623a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes      LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
3624a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    }
3625a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes    return std::distance(table_.begin(), it);
3626545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3627545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3628a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  size_t Size() const {
3629545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    return table_.size();
3630545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3631545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3632a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  void WriteTo(std::vector<uint8_t>& bytes) const {
363302e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier    for (const std::string& str : table_) {
363402e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier      const char* s = str.c_str();
36356d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      size_t s_len = CountModifiedUtf8Chars(s);
36366d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      UniquePtr<uint16_t> s_utf16(new uint16_t[s_len]);
36376d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      ConvertModifiedUtf8ToUtf16(s_utf16.get(), s);
36386d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers      JDWP::AppendUtf16BE(bytes, s_utf16.get(), s_len);
3639545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3640545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3641545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3642545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes private:
3643a8f93cbea2a094ac76e29d2ed17b8531b0a12cb6Elliott Hughes  std::set<std::string> table_;
3644545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(StringTable);
3645545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes};
3646545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3647545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes/*
3648545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The data we send to DDMS contains everything we have recorded.
3649545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
3650545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * Message header (all values big-endian):
3651545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) message header len (to allow future expansion); includes itself
3652545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) entry header len
3653545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (1b) stack frame len
3654545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of entries
3655545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (4b) offset to string table from start of message
3656545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of class name strings
3657545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of method name strings
3658545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (2b) number of source file name strings
3659545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * For each entry:
3660545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (4b) total allocation size
3661221229cb523f849f165fdafbf9785010963715daElliott Hughes *   (2b) thread id
3662545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (2b) allocated object's class name index
3663545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   (1b) stack depth
3664545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *   For each stack frame:
3665545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method's class name
3666545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method name
3667545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) method source file
3668545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *     (2b) line number, clipped to 32767; -2 if native; -1 if no source
3669545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) class name strings
3670545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) method name strings
3671545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * (xb) source file strings
3672545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
3673545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * As with other DDM traffic, strings are sent as a 4-byte length
3674545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * followed by UTF-16 data.
3675545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
3676545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We send up 16-bit unsigned indexes into string tables.  In theory there
3677b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes * can be (kMaxAllocRecordStackDepth * gAllocRecordMax) unique strings in
3678545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * each table, but in practice there should be far fewer.
3679545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
3680545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * The chief reason for using a string table here is to keep the size of
3681545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * the DDMS message to a minimum.  This is partly to make the protocol
3682545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * efficient, but also because we have to form the whole thing up all at
3683545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * once in a memory buffer.
3684545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes *
3685545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * We use separate string tables for class names, method names, and source
3686545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * files to keep the indexes small.  There will generally be no overlap
3687545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes * between the contents of these tables.
3688545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes */
3689545a064aca775ba801790fced3d713d8a87bfc61Elliott HughesjbyteArray Dbg::GetRecentAllocations() {
3690545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (false) {
3691545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    DumpRecentAllocations();
3692545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3693545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
369450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  Thread* self = Thread::Current();
369546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  std::vector<uint8_t> bytes;
369646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier  {
369746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    MutexLock mu(self, gAllocTrackerLock);
369846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
369946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 1: generate string tables.
370046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
370146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable class_names;
370246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable method_names;
370346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    StringTable filenames;
370446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
370546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    int count = gAllocRecordCount;
370646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    int idx = HeadIndex();
370746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
370846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
370946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
371046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      class_names.Add(ClassHelper(record->type).GetDescriptor());
371146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
371246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      MethodHelper mh;
371346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) {
3714ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom        mirror::ArtMethod* m = record->stack[i].method;
371546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        if (m != NULL) {
371646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          mh.ChangeMethod(m);
371746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          class_names.Add(mh.GetDeclaringClassDescriptor());
371846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          method_names.Add(mh.GetName());
371946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier          filenames.Add(mh.GetDeclaringClassSourceFile());
372046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        }
372146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      }
3722545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3723b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      idx = (idx + 1) & (gAllocRecordMax-1);
372446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    }
372546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
372646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    LOG(INFO) << "allocation records: " << gAllocRecordCount;
372746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
372846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
372946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // Part 2: Generate the output and store it in the buffer.
373046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    //
373146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
373246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) message header len (to allow future expansion); includes itself
373346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) entry header len
373446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (1b) stack frame len
373546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kMessageHeaderLen = 15;
373646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kEntryHeaderLen = 9;
373746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    const int kStackFrameLen = 8;
373846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kMessageHeaderLen);
373946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kEntryHeaderLen);
374046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append1BE(bytes, kStackFrameLen);
374146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
374246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of entries
374346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (4b) offset to string table from start of message
374446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of class name strings
374546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of method name strings
374646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (2b) number of source file name strings
374746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, gAllocRecordCount);
374846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    size_t string_table_offset = bytes.size();
37497934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom    JDWP::Append4BE(bytes, 0);  // We'll patch this later...
375046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, class_names.Size());
375146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, method_names.Size());
375246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Append2BE(bytes, filenames.Size());
375346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
375446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    count = gAllocRecordCount;
375546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    idx = HeadIndex();
375646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    while (count--) {
375746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // For each entry:
375846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (4b) total allocation size
375946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) thread id
376046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (2b) allocated object's class name index
376146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      // (1b) stack depth
376246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      AllocRecord* record = &recent_allocation_records_[idx];
376346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      size_t stack_depth = record->GetDepth();
3764590fee9e8972f872301c2d16a575d579ee564beeMathieu Chartier      ClassHelper kh(record->type);
376546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      size_t allocated_object_class_name_index = class_names.IndexOf(kh.GetDescriptor());
376646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append4BE(bytes, record->byte_count);
376746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, record->thin_lock_id);
376846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append2BE(bytes, allocated_object_class_name_index);
376946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      JDWP::Append1BE(bytes, stack_depth);
377046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
377146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      MethodHelper mh;
377246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier      for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
377346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // For each stack frame:
377446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method's class name
377546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method name
377646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) method source file
377746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        // (2b) line number, clipped to 32767; -2 if native; -1 if no source
377846e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        mh.ChangeMethod(record->stack[stack_frame].method);
377946e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        size_t class_name_index = class_names.IndexOf(mh.GetDeclaringClassDescriptor());
378046e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        size_t method_name_index = method_names.IndexOf(mh.GetName());
378146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        size_t file_name_index = filenames.IndexOf(mh.GetDeclaringClassSourceFile());
378246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, class_name_index);
378346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, method_name_index);
378446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, file_name_index);
378546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier        JDWP::Append2BE(bytes, record->stack[stack_frame].LineNumber());
3786545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes      }
378746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier
3788b1a5879d2c87a5af9f8dbd63a112b138b87135e4Elliott Hughes      idx = (idx + 1) & (gAllocRecordMax-1);
3789545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    }
3790545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
379146e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) class name strings
379246e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) method name strings
379346e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    // (xb) source file strings
379446e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    JDWP::Set4BE(&bytes[string_table_offset], bytes.size());
379546e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    class_names.WriteTo(bytes);
379646e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    method_names.WriteTo(bytes);
379746e811b07533a4c21f8c9abe22d22d6a3d8b18b0Mathieu Chartier    filenames.WriteTo(bytes);
3798545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
379950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers  JNIEnv* env = self->GetJniEnv();
3800545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  jbyteArray result = env->NewByteArray(bytes.size());
3801545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  if (result != NULL) {
3802545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes    env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
3803545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  }
3804545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes  return result;
3805545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes}
3806545a064aca775ba801790fced3d713d8a87bfc61Elliott Hughes
3807872d4ec7225444d9400d30f9027247deb91012fdElliott Hughes}  // namespace art
3808