blob: f218c4eaec8a9107517f604c7cf5cdd3387cdb30 [file] [log] [blame]
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "debugger.h"
18
Elliott Hughes3bb81562011-10-21 18:52:59 -070019#include <sys/uio.h>
20
Alex Light8c2b9292017-11-09 13:21:01 -080021#include <functional>
Andreas Gampef774a4e2017-07-06 22:15:18 -070022#include <memory>
Elliott Hughes545a0642011-11-08 19:10:03 -080023#include <set>
Andreas Gampef774a4e2017-07-06 22:15:18 -070024#include <vector>
Elliott Hughes545a0642011-11-08 19:10:03 -080025
Alex Lightb7c640d2019-03-20 15:52:13 -070026#include "android-base/macros.h"
Andreas Gampe46ee31b2016-12-14 10:11:49 -080027#include "android-base/stringprintf.h"
28
Ian Rogers166db042013-07-26 12:05:57 -070029#include "arch/context.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070030#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "art_method-inl.h"
Alex Lightfc588092020-01-23 15:39:08 -080032#include "base/endian_utils.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070033#include "base/enums.h"
Alex Lightfc588092020-01-23 15:39:08 -080034#include "base/logging.h"
Alex Lightb7c640d2019-03-20 15:52:13 -070035#include "base/memory_tool.h"
David Sehr67bf42e2018-02-26 16:43:04 -080036#include "base/safe_map.h"
Andreas Gampef774a4e2017-07-06 22:15:18 -070037#include "base/strlcpy.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010038#include "base/time_utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "class_linker-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070040#include "class_linker.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080041#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080042#include "dex/dex_file-inl.h"
43#include "dex/dex_file_annotations.h"
44#include "dex/dex_file_types.h"
45#include "dex/dex_instruction.h"
David Sehr0225f8e2018-01-31 08:52:24 +000046#include "dex/utf.h"
Mingyao Yang6ea1a0e2016-01-29 12:12:49 -080047#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070048#include "gc/accounting/card_table-inl.h"
Man Cao8c2ff642015-05-27 17:25:30 -070049#include "gc/allocation_record.h"
Andreas Gampe94c589d2017-12-27 12:43:01 -080050#include "gc/gc_cause.h"
Mathieu Chartieraa516822015-10-02 15:53:37 -070051#include "gc/scoped_gc_critical_section.h"
Andreas Gampe0c183382017-07-13 22:26:24 -070052#include "gc/space/bump_pointer_space-walk-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070053#include "gc/space/large_object_space.h"
54#include "gc/space/space-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070055#include "handle_scope-inl.h"
Alex Lightb7c640d2019-03-20 15:52:13 -070056#include "instrumentation.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010057#include "jni/jni_internal.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070058#include "jvalue-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070059#include "mirror/array-alloc-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070060#include "mirror/class-alloc-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061#include "mirror/class-inl.h"
Steven Morelande431e272017-07-18 16:53:49 -070062#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063#include "mirror/class_loader.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064#include "mirror/object-inl.h"
65#include "mirror/object_array-inl.h"
Andreas Gampefd63bbf2018-10-29 12:55:35 -070066#include "mirror/string-alloc-inl.h"
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070067#include "mirror/string-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068#include "mirror/throwable.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070069#include "nativehelper/scoped_local_ref.h"
70#include "nativehelper/scoped_primitive_array.h"
Nicolas Geoffray58cc1cb2017-11-20 13:27:29 +000071#include "oat_file.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070072#include "obj_ptr-inl.h"
Ian Rogers53b8b092014-03-13 23:45:53 -070073#include "reflection.h"
Alex Light55eccdf2019-10-07 13:51:13 +000074#include "reflective_handle.h"
75#include "reflective_handle_scope-inl.h"
David Srbecky28f6cff2018-10-16 15:07:28 +010076#include "runtime-inl.h"
Alex Lightfc588092020-01-23 15:39:08 -080077#include "runtime_callbacks.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070078#include "scoped_thread_state_change-inl.h"
Alex Lightfc588092020-01-23 15:39:08 -080079#include "scoped_thread_state_change.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070080#include "stack.h"
Alex Lightfc588092020-01-23 15:39:08 -080081#include "thread.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070082#include "thread_list.h"
Alex Lightfc588092020-01-23 15:39:08 -080083#include "thread_pool.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070084#include "well_known_classes.h"
Elliott Hughes475fc232011-10-25 15:00:35 -070085
Elliott Hughes872d4ec2011-10-21 17:07:15 -070086namespace art {
87
Andreas Gampe46ee31b2016-12-14 10:11:49 -080088using android::base::StringPrintf;
89
Man Cao8c2ff642015-05-27 17:25:30 -070090// Limit alloc_record_count to the 2BE value (64k-1) that is the limit of the current protocol.
Brian Carlstrom306db812014-09-05 13:01:41 -070091static uint16_t CappedAllocRecordCount(size_t alloc_record_count) {
Man Cao1ed11b92015-06-11 22:47:35 -070092 const size_t cap = 0xffff;
Man Cao8c2ff642015-05-27 17:25:30 -070093 if (alloc_record_count > cap) {
94 return cap;
Brian Carlstrom306db812014-09-05 13:01:41 -070095 }
96 return alloc_record_count;
97}
Elliott Hughes475fc232011-10-25 15:00:35 -070098
Elliott Hughes4ffd3132011-10-24 12:06:42 -070099// JDWP is allowed unless the Zygote forbids it.
100static bool gJdwpAllowed = true;
101
Elliott Hughes47fce012011-10-25 18:37:19 -0700102static bool gDdmThreadNotification = false;
103
Elliott Hughes767a1472011-10-26 18:49:02 -0700104// DDMS GC-related settings.
105static Dbg::HpifWhen gDdmHpifWhen = Dbg::HPIF_WHEN_NEVER;
106static Dbg::HpsgWhen gDdmHpsgWhen = Dbg::HPSG_WHEN_NEVER;
107static Dbg::HpsgWhat gDdmHpsgWhat;
108static Dbg::HpsgWhen gDdmNhsgWhen = Dbg::HPSG_WHEN_NEVER;
109static Dbg::HpsgWhat gDdmNhsgWhat;
110
Andreas Gampe04bbb5b2017-01-19 17:49:03 +0000111Dbg::DbgThreadLifecycleCallback Dbg::thread_lifecycle_callback_;
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700112
Elliott Hughes767a1472011-10-26 18:49:02 -0700113void Dbg::GcDidFinish() {
114 if (gDdmHpifWhen != HPIF_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700115 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700116 VLOG(jdwp) << "Sending heap info to DDM";
Elliott Hughes7162ad92011-10-27 14:08:42 -0700117 DdmSendHeapInfo(gDdmHpifWhen);
Elliott Hughes767a1472011-10-26 18:49:02 -0700118 }
119 if (gDdmHpsgWhen != HPSG_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700121 VLOG(jdwp) << "Dumping heap to DDM";
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700122 DdmSendHeapSegments(false);
Elliott Hughes767a1472011-10-26 18:49:02 -0700123 }
124 if (gDdmNhsgWhen != HPSG_WHEN_NEVER) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom4d466a82014-05-08 19:05:29 -0700126 VLOG(jdwp) << "Dumping native heap to DDM";
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700127 DdmSendHeapSegments(true);
Elliott Hughes767a1472011-10-26 18:49:02 -0700128 }
129}
130
Elliott Hughes4ffd3132011-10-24 12:06:42 -0700131void Dbg::SetJdwpAllowed(bool allowed) {
132 gJdwpAllowed = allowed;
133}
134
Leonard Mosescueb842212016-10-06 17:26:36 -0700135bool Dbg::IsJdwpAllowed() {
136 return gJdwpAllowed;
137}
138
Sebastien Hertz520633b2015-09-08 17:03:36 +0200139// Do we need to deoptimize the stack to handle an exception?
140bool Dbg::IsForcedInterpreterNeededForExceptionImpl(Thread* thread) {
Sebastien Hertz520633b2015-09-08 17:03:36 +0200141 // Deoptimization is required if at least one method in the stack needs it. However we
142 // skip frames that will be unwound (thus not executed).
Andreas Gampec7d878d2018-11-19 18:42:06 +0000143 bool needs_deoptimization = false;
144 StackVisitor::WalkStack(
145 [&](art::StackVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
146 // The visitor is meant to be used when handling exception from compiled code only.
147 CHECK(!visitor->IsShadowFrame()) << "We only expect to visit compiled frame: "
148 << ArtMethod::PrettyMethod(visitor->GetMethod());
149 ArtMethod* method = visitor->GetMethod();
150 if (method == nullptr) {
151 // We reach an upcall and don't need to deoptimize this part of the stack (ManagedFragment)
152 // so we can stop the visit.
153 DCHECK(!needs_deoptimization);
154 return false;
155 }
156 if (Runtime::Current()->GetInstrumentation()->InterpretOnly()) {
157 // We found a compiled frame in the stack but instrumentation is set to interpret
158 // everything: we need to deoptimize.
159 needs_deoptimization = true;
160 return false;
161 }
162 if (Runtime::Current()->GetInstrumentation()->IsDeoptimized(method)) {
163 // We found a deoptimized method in the stack.
164 needs_deoptimization = true;
165 return false;
166 }
167 ShadowFrame* frame = visitor->GetThread()->FindDebuggerShadowFrame(visitor->GetFrameId());
168 if (frame != nullptr) {
169 // The debugger allocated a ShadowFrame to update a variable in the stack: we need to
170 // deoptimize the stack to execute (and deallocate) this frame.
171 needs_deoptimization = true;
172 return false;
173 }
174 return true;
175 },
176 thread,
177 /* context= */ nullptr,
178 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames,
179 /* check_suspended */ true,
180 /* include_transitions */ true);
181 return needs_deoptimization;
Sebastien Hertz520633b2015-09-08 17:03:36 +0200182}
183
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700184
Alex Light8c2b9292017-11-09 13:21:01 -0800185bool Dbg::DdmHandleChunk(JNIEnv* env,
186 uint32_t type,
187 const ArrayRef<const jbyte>& data,
188 /*out*/uint32_t* out_type,
189 /*out*/std::vector<uint8_t>* out_data) {
190 ScopedLocalRef<jbyteArray> dataArray(env, env->NewByteArray(data.size()));
Ian Rogersc0542af2014-09-03 16:16:56 -0700191 if (dataArray.get() == nullptr) {
Alex Light8c2b9292017-11-09 13:21:01 -0800192 LOG(WARNING) << "byte[] allocation failed: " << data.size();
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700193 env->ExceptionClear();
194 return false;
195 }
Alex Light8c2b9292017-11-09 13:21:01 -0800196 env->SetByteArrayRegion(dataArray.get(),
197 0,
198 data.size(),
199 reinterpret_cast<const jbyte*>(data.data()));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700200 // Call "private static Chunk dispatch(int type, byte[] data, int offset, int length)".
Alex Light8c2b9292017-11-09 13:21:01 -0800201 ScopedLocalRef<jobject> chunk(
202 env,
203 env->CallStaticObjectMethod(
204 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
205 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch,
206 type, dataArray.get(), 0, data.size()));
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700207 if (env->ExceptionCheck()) {
Alex Light1eeefa62018-03-19 13:47:56 -0700208 Thread* self = Thread::Current();
209 ScopedObjectAccess soa(self);
210 LOG(INFO) << StringPrintf("Exception thrown by dispatcher for 0x%08x", type) << std::endl
211 << self->GetException()->Dump();
212 self->ClearException();
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700213 return false;
214 }
215
Ian Rogersc0542af2014-09-03 16:16:56 -0700216 if (chunk.get() == nullptr) {
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700217 return false;
218 }
219
220 /*
221 * Pull the pieces out of the chunk. We copy the results into a
222 * newly-allocated buffer that the caller can free. We don't want to
223 * continue using the Chunk object because nothing has a reference to it.
224 *
225 * We could avoid this by returning type/data/offset/length and having
226 * the caller be aware of the object lifetime issues, but that
Elliott Hughes81ff3182012-03-23 20:35:56 -0700227 * integrates the JDWP code more tightly into the rest of the runtime, and doesn't work
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700228 * if we have responses for multiple chunks.
229 *
230 * So we're pretty much stuck with copying data around multiple times.
231 */
Alex Light8c2b9292017-11-09 13:21:01 -0800232 ScopedLocalRef<jbyteArray> replyData(
233 env,
234 reinterpret_cast<jbyteArray>(
235 env->GetObjectField(
236 chunk.get(), WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_data)));
237 jint offset = env->GetIntField(chunk.get(),
238 WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_offset);
239 jint length = env->GetIntField(chunk.get(),
240 WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_length);
241 *out_type = env->GetIntField(chunk.get(),
242 WellKnownClasses::org_apache_harmony_dalvik_ddmc_Chunk_type);
Elliott Hughesf6a1e1e2011-10-25 16:28:04 -0700243
Alex Light8c2b9292017-11-09 13:21:01 -0800244 VLOG(jdwp) << StringPrintf("DDM reply: type=0x%08x data=%p offset=%d length=%d",
245 type,
246 replyData.get(),
247 offset,
248 length);
Alex Light8c2b9292017-11-09 13:21:01 -0800249 out_data->resize(length);
250 env->GetByteArrayRegion(replyData.get(),
251 offset,
252 length,
253 reinterpret_cast<jbyte*>(out_data->data()));
Alex Light6f2a6342017-12-12 09:55:05 -0800254
Alex Lighte5463a82017-12-12 13:33:28 -0800255 if (env->ExceptionCheck()) {
Alex Light1eeefa62018-03-19 13:47:56 -0700256 Thread* self = Thread::Current();
257 ScopedObjectAccess soa(self);
Alex Lighte5463a82017-12-12 13:33:28 -0800258 LOG(INFO) << StringPrintf("Exception thrown when reading response data from dispatcher 0x%08x",
Alex Light1eeefa62018-03-19 13:47:56 -0700259 type) << std::endl << self->GetException()->Dump();
260 self->ClearException();
Alex Lighte5463a82017-12-12 13:33:28 -0800261 return false;
262 }
263
Alex Light8c2b9292017-11-09 13:21:01 -0800264 return true;
265}
266
Elliott Hughesa2155262011-11-16 16:26:58 -0800267void Dbg::DdmBroadcast(bool connect) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800268 VLOG(jdwp) << "Broadcasting DDM " << (connect ? "connect" : "disconnect") << "...";
Elliott Hughes47fce012011-10-25 18:37:19 -0700269
270 Thread* self = Thread::Current();
Ian Rogers50b35e22012-10-04 10:09:15 -0700271 if (self->GetState() != kRunnable) {
272 LOG(ERROR) << "DDM broadcast in thread state " << self->GetState();
273 /* try anyway? */
Elliott Hughes47fce012011-10-25 18:37:19 -0700274 }
275
276 JNIEnv* env = self->GetJniEnv();
Elliott Hughes47fce012011-10-25 18:37:19 -0700277 jint event = connect ? 1 /*DdmServer.CONNECTED*/ : 2 /*DdmServer.DISCONNECTED*/;
Elliott Hugheseac76672012-05-24 21:56:51 -0700278 env->CallStaticVoidMethod(WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer,
279 WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_broadcast,
280 event);
Elliott Hughes47fce012011-10-25 18:37:19 -0700281 if (env->ExceptionCheck()) {
282 LOG(ERROR) << "DdmServer.broadcast " << event << " failed";
283 env->ExceptionDescribe();
284 env->ExceptionClear();
285 }
286}
287
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700288void Dbg::DdmConnected() {
Elliott Hughesa2155262011-11-16 16:26:58 -0800289 Dbg::DdmBroadcast(true);
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700290}
291
292void Dbg::DdmDisconnected() {
Elliott Hughesa2155262011-11-16 16:26:58 -0800293 Dbg::DdmBroadcast(false);
Elliott Hughes47fce012011-10-25 18:37:19 -0700294 gDdmThreadNotification = false;
295}
296
Alex Lightfc588092020-01-23 15:39:08 -0800297
Elliott Hughes47fce012011-10-25 18:37:19 -0700298/*
Elliott Hughes82188472011-11-07 18:11:48 -0800299 * Send a notification when a thread starts, stops, or changes its name.
Elliott Hughes47fce012011-10-25 18:37:19 -0700300 *
301 * Because we broadcast the full set of threads when the notifications are
302 * first enabled, it's possible for "thread" to be actively executing.
303 */
Elliott Hughes82188472011-11-07 18:11:48 -0800304void Dbg::DdmSendThreadNotification(Thread* t, uint32_t type) {
Alex Lightfc588092020-01-23 15:39:08 -0800305 Locks::mutator_lock_->AssertNotExclusiveHeld(Thread::Current());
Elliott Hughes47fce012011-10-25 18:37:19 -0700306 if (!gDdmThreadNotification) {
307 return;
308 }
309
Alex Light772099a2017-11-21 14:05:04 -0800310 RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
Elliott Hughes82188472011-11-07 18:11:48 -0800311 if (type == CHUNK_TYPE("THDE")) {
Elliott Hughes47fce012011-10-25 18:37:19 -0700312 uint8_t buf[4];
Alex Lightfc588092020-01-23 15:39:08 -0800313 Set4BE(&buf[0], t->GetThreadId());
Alex Light772099a2017-11-21 14:05:04 -0800314 cb->DdmPublishChunk(CHUNK_TYPE("THDE"), ArrayRef<const uint8_t>(buf));
Elliott Hughes82188472011-11-07 18:11:48 -0800315 } else {
316 CHECK(type == CHUNK_TYPE("THCR") || type == CHUNK_TYPE("THNM")) << type;
Alex Lightfc588092020-01-23 15:39:08 -0800317 StackHandleScope<1> hs(Thread::Current());
Andreas Gampe08883de2016-11-08 13:20:52 -0800318 Handle<mirror::String> name(hs.NewHandle(t->GetThreadName()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800319 size_t char_count = (name != nullptr) ? name->GetLength() : 0;
320 const jchar* chars = (name != nullptr) ? name->GetValue() : nullptr;
321 bool is_compressed = (name != nullptr) ? name->IsCompressed() : false;
Elliott Hughes82188472011-11-07 18:11:48 -0800322
Elliott Hughes21f32d72011-11-09 17:44:13 -0800323 std::vector<uint8_t> bytes;
Alex Lightfc588092020-01-23 15:39:08 -0800324 Append4BE(bytes, t->GetThreadId());
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700325 if (is_compressed) {
326 const uint8_t* chars_compressed = name->GetValueCompressed();
Alex Lightfc588092020-01-23 15:39:08 -0800327 AppendUtf16CompressedBE(bytes, chars_compressed, char_count);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700328 } else {
Alex Lightfc588092020-01-23 15:39:08 -0800329 AppendUtf16BE(bytes, chars, char_count);
jessicahandojo3aaa37b2016-07-29 14:46:37 -0700330 }
Elliott Hughes21f32d72011-11-09 17:44:13 -0800331 CHECK_EQ(bytes.size(), char_count*2 + sizeof(uint32_t)*2);
Alex Light772099a2017-11-21 14:05:04 -0800332 cb->DdmPublishChunk(type, ArrayRef<const uint8_t>(bytes));
Elliott Hughes47fce012011-10-25 18:37:19 -0700333 }
334}
335
Elliott Hughes47fce012011-10-25 18:37:19 -0700336void Dbg::DdmSetThreadNotification(bool enable) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700337 // Enable/disable thread notifications.
Elliott Hughes47fce012011-10-25 18:37:19 -0700338 gDdmThreadNotification = enable;
339 if (enable) {
Alex Lightfc588092020-01-23 15:39:08 -0800340 // Use a Checkpoint to cause every currently running thread to send their own notification when
341 // able. We then wait for every thread thread active at the time to post the creation
342 // notification. Threads created later will send this themselves.
Ian Rogers50b35e22012-10-04 10:09:15 -0700343 Thread* self = Thread::Current();
Alex Lightfc588092020-01-23 15:39:08 -0800344 ScopedObjectAccess soa(self);
345 Barrier finish_barrier(0);
346 FunctionClosure fc([&](Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_) {
347 Thread* cls_self = Thread::Current();
348 Locks::mutator_lock_->AssertSharedHeld(cls_self);
349 Dbg::DdmSendThreadNotification(thread, CHUNK_TYPE("THCR"));
350 finish_barrier.Pass(cls_self);
351 });
352 size_t checkpoints = Runtime::Current()->GetThreadList()->RunCheckpoint(&fc);
353 ScopedThreadSuspension sts(self, ThreadState::kWaitingForCheckPointsToRun);
354 finish_barrier.Increment(self, checkpoints);
Elliott Hughes47fce012011-10-25 18:37:19 -0700355 }
356}
357
Elliott Hughesa2155262011-11-16 16:26:58 -0800358void Dbg::PostThreadStartOrStop(Thread* t, uint32_t type) {
Elliott Hughes82188472011-11-07 18:11:48 -0800359 Dbg::DdmSendThreadNotification(t, type);
Elliott Hughes47fce012011-10-25 18:37:19 -0700360}
361
362void Dbg::PostThreadStart(Thread* t) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800363 Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THCR"));
Elliott Hughes47fce012011-10-25 18:37:19 -0700364}
365
366void Dbg::PostThreadDeath(Thread* t) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800367 Dbg::PostThreadStartOrStop(t, CHUNK_TYPE("THDE"));
Elliott Hughes872d4ec2011-10-21 17:07:15 -0700368}
369
Elliott Hughes767a1472011-10-26 18:49:02 -0700370int Dbg::DdmHandleHpifChunk(HpifWhen when) {
371 if (when == HPIF_WHEN_NOW) {
Elliott Hughes7162ad92011-10-27 14:08:42 -0700372 DdmSendHeapInfo(when);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700373 return 1;
Elliott Hughes767a1472011-10-26 18:49:02 -0700374 }
375
376 if (when != HPIF_WHEN_NEVER && when != HPIF_WHEN_NEXT_GC && when != HPIF_WHEN_EVERY_GC) {
377 LOG(ERROR) << "invalid HpifWhen value: " << static_cast<int>(when);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700378 return 0;
Elliott Hughes767a1472011-10-26 18:49:02 -0700379 }
380
381 gDdmHpifWhen = when;
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700382 return 1;
Elliott Hughes767a1472011-10-26 18:49:02 -0700383}
384
385bool Dbg::DdmHandleHpsgNhsgChunk(Dbg::HpsgWhen when, Dbg::HpsgWhat what, bool native) {
386 if (when != HPSG_WHEN_NEVER && when != HPSG_WHEN_EVERY_GC) {
387 LOG(ERROR) << "invalid HpsgWhen value: " << static_cast<int>(when);
388 return false;
389 }
390
391 if (what != HPSG_WHAT_MERGED_OBJECTS && what != HPSG_WHAT_DISTINCT_OBJECTS) {
392 LOG(ERROR) << "invalid HpsgWhat value: " << static_cast<int>(what);
393 return false;
394 }
395
396 if (native) {
397 gDdmNhsgWhen = when;
398 gDdmNhsgWhat = what;
399 } else {
400 gDdmHpsgWhen = when;
401 gDdmHpsgWhat = what;
402 }
403 return true;
404}
405
Elliott Hughes7162ad92011-10-27 14:08:42 -0700406void Dbg::DdmSendHeapInfo(HpifWhen reason) {
407 // If there's a one-shot 'when', reset it.
408 if (reason == gDdmHpifWhen) {
409 if (gDdmHpifWhen == HPIF_WHEN_NEXT_GC) {
410 gDdmHpifWhen = HPIF_WHEN_NEVER;
411 }
412 }
413
414 /*
415 * Chunk HPIF (client --> server)
416 *
417 * Heap Info. General information about the heap,
418 * suitable for a summary display.
419 *
420 * [u4]: number of heaps
421 *
422 * For each heap:
423 * [u4]: heap ID
424 * [u8]: timestamp in ms since Unix epoch
425 * [u1]: capture reason (same as 'when' value from server)
426 * [u4]: max heap size in bytes (-Xmx)
427 * [u4]: current heap size in bytes
428 * [u4]: current number of bytes allocated
429 * [u4]: current number of objects allocated
430 */
431 uint8_t heap_count = 1;
Ian Rogers1d54e732013-05-02 21:10:01 -0700432 gc::Heap* heap = Runtime::Current()->GetHeap();
Elliott Hughes21f32d72011-11-09 17:44:13 -0800433 std::vector<uint8_t> bytes;
Alex Lightfc588092020-01-23 15:39:08 -0800434 Append4BE(bytes, heap_count);
435 Append4BE(bytes, 1); // Heap id (bogus; we only have one heap).
436 Append8BE(bytes, MilliTime());
437 Append1BE(bytes, reason);
438 Append4BE(bytes, heap->GetMaxMemory()); // Max allowed heap size in bytes.
439 Append4BE(bytes, heap->GetTotalMemory()); // Current heap size in bytes.
440 Append4BE(bytes, heap->GetBytesAllocated());
441 Append4BE(bytes, heap->GetObjectsAllocated());
Elliott Hughes21f32d72011-11-09 17:44:13 -0800442 CHECK_EQ(bytes.size(), 4U + (heap_count * (4 + 8 + 1 + 4 + 4 + 4 + 4)));
Alex Light772099a2017-11-21 14:05:04 -0800443 Runtime::Current()->GetRuntimeCallbacks()->DdmPublishChunk(CHUNK_TYPE("HPIF"),
444 ArrayRef<const uint8_t>(bytes));
Elliott Hughes767a1472011-10-26 18:49:02 -0700445}
446
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700447enum HpsgSolidity {
448 SOLIDITY_FREE = 0,
449 SOLIDITY_HARD = 1,
450 SOLIDITY_SOFT = 2,
451 SOLIDITY_WEAK = 3,
452 SOLIDITY_PHANTOM = 4,
453 SOLIDITY_FINALIZABLE = 5,
454 SOLIDITY_SWEEP = 6,
455};
456
457enum HpsgKind {
458 KIND_OBJECT = 0,
459 KIND_CLASS_OBJECT = 1,
460 KIND_ARRAY_1 = 2,
461 KIND_ARRAY_2 = 3,
462 KIND_ARRAY_4 = 4,
463 KIND_ARRAY_8 = 5,
464 KIND_UNKNOWN = 6,
465 KIND_NATIVE = 7,
466};
467
468#define HPSG_PARTIAL (1<<7)
469#define HPSG_STATE(solidity, kind) ((uint8_t)((((kind) & 0x7) << 3) | ((solidity) & 0x7)))
470
Ian Rogers30fab402012-01-23 15:43:46 -0800471class HeapChunkContext {
472 public:
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700473 // Maximum chunk size. Obtain this from the formula:
474 // (((maximum_heap_size / ALLOCATION_UNIT_SIZE) + 255) / 256) * 2
475 HeapChunkContext(bool merge, bool native)
Ian Rogers30fab402012-01-23 15:43:46 -0800476 : buf_(16384 - 16),
477 type_(0),
Mathieu Chartier36dab362014-07-30 14:59:56 -0700478 chunk_overhead_(0) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700479 Reset();
480 if (native) {
Ian Rogers30fab402012-01-23 15:43:46 -0800481 type_ = CHUNK_TYPE("NHSG");
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700482 } else {
Ian Rogers30fab402012-01-23 15:43:46 -0800483 type_ = merge ? CHUNK_TYPE("HPSG") : CHUNK_TYPE("HPSO");
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700484 }
485 }
486
487 ~HeapChunkContext() {
Ian Rogers30fab402012-01-23 15:43:46 -0800488 if (p_ > &buf_[0]) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700489 Flush();
490 }
491 }
492
Mathieu Chartier36dab362014-07-30 14:59:56 -0700493 void SetChunkOverhead(size_t chunk_overhead) {
494 chunk_overhead_ = chunk_overhead;
495 }
496
497 void ResetStartOfNextChunk() {
498 startOfNextMemoryChunk_ = nullptr;
499 }
500
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700501 void EnsureHeader(const void* chunk_ptr) {
Ian Rogers30fab402012-01-23 15:43:46 -0800502 if (!needHeader_) {
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700503 return;
504 }
505
506 // Start a new HPSx chunk.
Alex Lightfc588092020-01-23 15:39:08 -0800507 Write4BE(&p_, 1); // Heap id (bogus; we only have one heap).
508 Write1BE(&p_, 8); // Size of allocation unit, in bytes.
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700509
Alex Lightfc588092020-01-23 15:39:08 -0800510 Write4BE(&p_, reinterpret_cast<uintptr_t>(chunk_ptr)); // virtual address of segment start.
511 Write4BE(&p_, 0); // offset of this piece (relative to the virtual address).
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700512 // [u4]: length of piece, in allocation units
513 // We won't know this until we're done, so save the offset and stuff in a dummy value.
Ian Rogers30fab402012-01-23 15:43:46 -0800514 pieceLenField_ = p_;
Alex Lightfc588092020-01-23 15:39:08 -0800515 Write4BE(&p_, 0x55555555);
Ian Rogers30fab402012-01-23 15:43:46 -0800516 needHeader_ = false;
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700517 }
518
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700519 void Flush() REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700520 if (pieceLenField_ == nullptr) {
Ian Rogersd636b062013-01-18 17:51:18 -0800521 // Flush immediately post Reset (maybe back-to-back Flush). Ignore.
522 CHECK(needHeader_);
523 return;
524 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700525 // Patch the "length of piece" field.
Ian Rogers30fab402012-01-23 15:43:46 -0800526 CHECK_LE(&buf_[0], pieceLenField_);
527 CHECK_LE(pieceLenField_, p_);
Alex Lightfc588092020-01-23 15:39:08 -0800528 Set4BE(pieceLenField_, totalAllocationUnits_);
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700529
Alex Light772099a2017-11-21 14:05:04 -0800530 ArrayRef<const uint8_t> out(&buf_[0], p_ - &buf_[0]);
531 Runtime::Current()->GetRuntimeCallbacks()->DdmPublishChunk(type_, out);
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700532 Reset();
533 }
534
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800535 static void HeapChunkJavaCallback(void* start, void* end, size_t used_bytes, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700536 REQUIRES_SHARED(Locks::heap_bitmap_lock_,
Ian Rogersb726dcb2012-09-05 08:57:23 -0700537 Locks::mutator_lock_) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800538 reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkJavaCallback(start, end, used_bytes);
539 }
540
541 static void HeapChunkNativeCallback(void* start, void* end, size_t used_bytes, void* arg)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700542 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800543 reinterpret_cast<HeapChunkContext*>(arg)->HeapChunkNativeCallback(start, end, used_bytes);
Elliott Hughesa2155262011-11-16 16:26:58 -0800544 }
545
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700546 private:
Elliott Hughesa2155262011-11-16 16:26:58 -0800547 enum { ALLOCATION_UNIT_SIZE = 8 };
548
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700549 void Reset() {
Ian Rogers30fab402012-01-23 15:43:46 -0800550 p_ = &buf_[0];
Mathieu Chartier36dab362014-07-30 14:59:56 -0700551 ResetStartOfNextChunk();
Ian Rogers30fab402012-01-23 15:43:46 -0800552 totalAllocationUnits_ = 0;
553 needHeader_ = true;
Ian Rogersc0542af2014-09-03 16:16:56 -0700554 pieceLenField_ = nullptr;
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700555 }
556
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800557 bool IsNative() const {
558 return type_ == CHUNK_TYPE("NHSG");
559 }
560
561 // Returns true if the object is not an empty chunk.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700562 bool ProcessRecord(void* start, size_t used_bytes) REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers30fab402012-01-23 15:43:46 -0800563 // Note: heap call backs cannot manipulate the heap upon which they are crawling, care is taken
564 // in the following code not to allocate memory, by ensuring buf_ is of the correct size
Ian Rogers15bf2d32012-08-28 17:33:04 -0700565 if (used_bytes == 0) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800566 if (start == nullptr) {
567 // Reset for start of new heap.
568 startOfNextMemoryChunk_ = nullptr;
569 Flush();
570 }
571 // Only process in use memory so that free region information
572 // also includes dlmalloc book keeping.
573 return false;
Elliott Hughesa2155262011-11-16 16:26:58 -0800574 }
Ian Rogersc0542af2014-09-03 16:16:56 -0700575 if (startOfNextMemoryChunk_ != nullptr) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800576 // Transmit any pending free memory. Native free memory of over kMaxFreeLen could be because
577 // of the use of mmaps, so don't report. If not free memory then start a new segment.
578 bool flush = true;
579 if (start > startOfNextMemoryChunk_) {
580 const size_t kMaxFreeLen = 2 * kPageSize;
581 void* free_start = startOfNextMemoryChunk_;
582 void* free_end = start;
583 const size_t free_len =
584 reinterpret_cast<uintptr_t>(free_end) - reinterpret_cast<uintptr_t>(free_start);
585 if (!IsNative() || free_len < kMaxFreeLen) {
586 AppendChunk(HPSG_STATE(SOLIDITY_FREE, 0), free_start, free_len, IsNative());
587 flush = false;
Ian Rogers15bf2d32012-08-28 17:33:04 -0700588 }
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800589 }
590 if (flush) {
591 startOfNextMemoryChunk_ = nullptr;
592 Flush();
593 }
Ian Rogers15bf2d32012-08-28 17:33:04 -0700594 }
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800595 return true;
Ian Rogers15bf2d32012-08-28 17:33:04 -0700596 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800597
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800598 void HeapChunkNativeCallback(void* start, void* /*end*/, size_t used_bytes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700599 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800600 if (ProcessRecord(start, used_bytes)) {
601 uint8_t state = ExamineNativeObject(start);
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700602 AppendChunk(state, start, used_bytes + chunk_overhead_, /*is_native=*/ true);
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800603 startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_;
604 }
605 }
606
607 void HeapChunkJavaCallback(void* start, void* /*end*/, size_t used_bytes)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700608 REQUIRES_SHARED(Locks::heap_bitmap_lock_, Locks::mutator_lock_) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800609 if (ProcessRecord(start, used_bytes)) {
610 // Determine the type of this chunk.
611 // OLD-TODO: if context.merge, see if this chunk is different from the last chunk.
612 // If it's the same, we should combine them.
613 uint8_t state = ExamineJavaObject(reinterpret_cast<mirror::Object*>(start));
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700614 AppendChunk(state, start, used_bytes + chunk_overhead_, /*is_native=*/ false);
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800615 startOfNextMemoryChunk_ = reinterpret_cast<char*>(start) + used_bytes + chunk_overhead_;
616 }
617 }
618
619 void AppendChunk(uint8_t state, void* ptr, size_t length, bool is_native)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700620 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers15bf2d32012-08-28 17:33:04 -0700621 // Make sure there's enough room left in the buffer.
622 // We need to use two bytes for every fractional 256 allocation units used by the chunk plus
623 // 17 bytes for any header.
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800624 const size_t needed = ((RoundUp(length / ALLOCATION_UNIT_SIZE, 256) / 256) * 2) + 17;
625 size_t byte_left = &buf_.back() - p_;
626 if (byte_left < needed) {
627 if (is_native) {
Pavel Vyssotski7522c742014-12-08 13:38:26 +0600628 // Cannot trigger memory allocation while walking native heap.
Pavel Vyssotski7522c742014-12-08 13:38:26 +0600629 return;
630 }
Ian Rogers15bf2d32012-08-28 17:33:04 -0700631 Flush();
632 }
633
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800634 byte_left = &buf_.back() - p_;
635 if (byte_left < needed) {
Ian Rogers15bf2d32012-08-28 17:33:04 -0700636 LOG(WARNING) << "Chunk is too big to transmit (chunk_len=" << length << ", "
637 << needed << " bytes)";
638 return;
639 }
640 EnsureHeader(ptr);
Elliott Hughesa2155262011-11-16 16:26:58 -0800641 // Write out the chunk description.
Ian Rogers15bf2d32012-08-28 17:33:04 -0700642 length /= ALLOCATION_UNIT_SIZE; // Convert to allocation units.
643 totalAllocationUnits_ += length;
644 while (length > 256) {
Ian Rogers30fab402012-01-23 15:43:46 -0800645 *p_++ = state | HPSG_PARTIAL;
646 *p_++ = 255; // length - 1
Ian Rogers15bf2d32012-08-28 17:33:04 -0700647 length -= 256;
Elliott Hughesa2155262011-11-16 16:26:58 -0800648 }
Ian Rogers30fab402012-01-23 15:43:46 -0800649 *p_++ = state;
Ian Rogers15bf2d32012-08-28 17:33:04 -0700650 *p_++ = length - 1;
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700651 }
652
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700653 uint8_t ExamineNativeObject(const void* p) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800654 return p == nullptr ? HPSG_STATE(SOLIDITY_FREE, 0) : HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
655 }
656
Vladimir Marko83114892019-04-11 13:05:50 +0100657 uint8_t ExamineJavaObject(ObjPtr<mirror::Object> o)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700658 REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) {
Ian Rogersc0542af2014-09-03 16:16:56 -0700659 if (o == nullptr) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800660 return HPSG_STATE(SOLIDITY_FREE, 0);
661 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800662 // It's an allocated chunk. Figure out what it is.
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800663 gc::Heap* heap = Runtime::Current()->GetHeap();
664 if (!heap->IsLiveObjectLocked(o)) {
665 LOG(ERROR) << "Invalid object in managed heap: " << o;
Elliott Hughesa2155262011-11-16 16:26:58 -0800666 return HPSG_STATE(SOLIDITY_HARD, KIND_NATIVE);
667 }
Vladimir Marko4617d582019-03-28 13:48:31 +0000668 ObjPtr<mirror::Class> c = o->GetClass();
Ian Rogersc0542af2014-09-03 16:16:56 -0700669 if (c == nullptr) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800670 // The object was probably just created but hasn't been initialized yet.
671 return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
672 }
Vladimir Marko4617d582019-03-28 13:48:31 +0000673 if (!heap->IsValidObjectAddress(c.Ptr())) {
Ian Rogers15bf2d32012-08-28 17:33:04 -0700674 LOG(ERROR) << "Invalid class for managed heap object: " << o << " " << c;
Elliott Hughesa2155262011-11-16 16:26:58 -0800675 return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
676 }
Mathieu Chartierf26e1b32015-01-29 10:47:10 -0800677 if (c->GetClass() == nullptr) {
678 LOG(ERROR) << "Null class of class " << c << " for object " << o;
679 return HPSG_STATE(SOLIDITY_HARD, KIND_UNKNOWN);
680 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800681 if (c->IsClassClass()) {
682 return HPSG_STATE(SOLIDITY_HARD, KIND_CLASS_OBJECT);
683 }
Elliott Hughesa2155262011-11-16 16:26:58 -0800684 if (c->IsArrayClass()) {
Elliott Hughesa2155262011-11-16 16:26:58 -0800685 switch (c->GetComponentSize()) {
686 case 1: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_1);
687 case 2: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_2);
688 case 4: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_4);
689 case 8: return HPSG_STATE(SOLIDITY_HARD, KIND_ARRAY_8);
690 }
691 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700692 return HPSG_STATE(SOLIDITY_HARD, KIND_OBJECT);
693 }
694
Ian Rogers30fab402012-01-23 15:43:46 -0800695 std::vector<uint8_t> buf_;
696 uint8_t* p_;
697 uint8_t* pieceLenField_;
Ian Rogers15bf2d32012-08-28 17:33:04 -0700698 void* startOfNextMemoryChunk_;
Ian Rogers30fab402012-01-23 15:43:46 -0800699 size_t totalAllocationUnits_;
700 uint32_t type_;
Ian Rogers30fab402012-01-23 15:43:46 -0800701 bool needHeader_;
Mathieu Chartier36dab362014-07-30 14:59:56 -0700702 size_t chunk_overhead_;
Ian Rogers30fab402012-01-23 15:43:46 -0800703
Elliott Hughesa2155262011-11-16 16:26:58 -0800704 DISALLOW_COPY_AND_ASSIGN(HeapChunkContext);
705};
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700706
Alex Lightfc588092020-01-23 15:39:08 -0800707
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700708void Dbg::DdmSendHeapSegments(bool native) {
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800709 Dbg::HpsgWhen when = native ? gDdmNhsgWhen : gDdmHpsgWhen;
710 Dbg::HpsgWhat what = native ? gDdmNhsgWhat : gDdmHpsgWhat;
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700711 if (when == HPSG_WHEN_NEVER) {
712 return;
713 }
Alex Light772099a2017-11-21 14:05:04 -0800714 RuntimeCallbacks* cb = Runtime::Current()->GetRuntimeCallbacks();
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700715 // Figure out what kind of chunks we'll be sending.
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800716 CHECK(what == HPSG_WHAT_MERGED_OBJECTS || what == HPSG_WHAT_DISTINCT_OBJECTS)
717 << static_cast<int>(what);
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700718
719 // First, send a heap start chunk.
720 uint8_t heap_id[4];
Alex Lightfc588092020-01-23 15:39:08 -0800721 Set4BE(&heap_id[0], 1); // Heap id (bogus; we only have one heap).
Alex Light772099a2017-11-21 14:05:04 -0800722 cb->DdmPublishChunk(native ? CHUNK_TYPE("NHST") : CHUNK_TYPE("HPST"),
723 ArrayRef<const uint8_t>(heap_id));
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700724 Thread* self = Thread::Current();
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700725 Locks::mutator_lock_->AssertSharedHeld(self);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700726
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700727 // Send a series of heap segment chunks.
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800728 HeapChunkContext context(what == HPSG_WHAT_MERGED_OBJECTS, native);
Andreas Gampe0c183382017-07-13 22:26:24 -0700729 auto bump_pointer_space_visitor = [&](mirror::Object* obj)
730 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) {
731 const size_t size = RoundUp(obj->SizeOf(), kObjectAlignment);
732 HeapChunkContext::HeapChunkJavaCallback(
733 obj, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(obj) + size), size, &context);
734 };
Elliott Hughesa2155262011-11-16 16:26:58 -0800735 if (native) {
Dimitry Ivanove6465bc2015-12-14 18:55:02 -0800736 UNIMPLEMENTED(WARNING) << "Native heap inspection is not supported";
Elliott Hughesa2155262011-11-16 16:26:58 -0800737 } else {
Ian Rogers1d54e732013-05-02 21:10:01 -0700738 gc::Heap* heap = Runtime::Current()->GetHeap();
Mathieu Chartier36dab362014-07-30 14:59:56 -0700739 for (const auto& space : heap->GetContinuousSpaces()) {
740 if (space->IsDlMallocSpace()) {
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -0700741 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier36dab362014-07-30 14:59:56 -0700742 // dlmalloc's chunk header is 2 * sizeof(size_t), but if the previous chunk is in use for an
743 // allocation then the first sizeof(size_t) may belong to it.
744 context.SetChunkOverhead(sizeof(size_t));
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800745 space->AsDlMallocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context);
Mathieu Chartier36dab362014-07-30 14:59:56 -0700746 } else if (space->IsRosAllocSpace()) {
747 context.SetChunkOverhead(0);
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -0700748 // Need to acquire the mutator lock before the heap bitmap lock with exclusive access since
749 // RosAlloc's internal logic doesn't know to release and reacquire the heap bitmap lock.
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700750 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700751 ScopedSuspendAll ssa(__FUNCTION__);
752 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
753 space->AsRosAllocSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context);
Mathieu Chartier36dab362014-07-30 14:59:56 -0700754 } else if (space->IsBumpPointerSpace()) {
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -0700755 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartier36dab362014-07-30 14:59:56 -0700756 context.SetChunkOverhead(0);
Andreas Gampe0c183382017-07-13 22:26:24 -0700757 space->AsBumpPointerSpace()->Walk(bump_pointer_space_visitor);
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800758 HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context);
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800759 } else if (space->IsRegionSpace()) {
760 heap->IncrementDisableMovingGC(self);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700761 {
762 ScopedThreadSuspension sts(self, kSuspended);
Mathieu Chartier4f55e222015-09-04 13:26:21 -0700763 ScopedSuspendAll ssa(__FUNCTION__);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700764 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
765 context.SetChunkOverhead(0);
Andreas Gampe0c183382017-07-13 22:26:24 -0700766 space->AsRegionSpace()->Walk(bump_pointer_space_visitor);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700767 HeapChunkContext::HeapChunkJavaCallback(nullptr, nullptr, 0, &context);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700768 }
Hiroshi Yamauchi2cd334a2015-01-09 14:03:35 -0800769 heap->DecrementDisableMovingGC(self);
Mathieu Chartier36dab362014-07-30 14:59:56 -0700770 } else {
771 UNIMPLEMENTED(WARNING) << "Not counting objects in space " << *space;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700772 }
Mathieu Chartier36dab362014-07-30 14:59:56 -0700773 context.ResetStartOfNextChunk();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700774 }
Mathieu Chartier4c69d7f2014-10-10 12:45:50 -0700775 ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700776 // Walk the large objects, these are not in the AllocSpace.
Mathieu Chartier36dab362014-07-30 14:59:56 -0700777 context.SetChunkOverhead(0);
Mathieu Chartierbc689b72014-12-14 17:01:31 -0800778 heap->GetLargeObjectsSpace()->Walk(HeapChunkContext::HeapChunkJavaCallback, &context);
Elliott Hughesa2155262011-11-16 16:26:58 -0800779 }
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700780
781 // Finally, send a heap end chunk.
Alex Light772099a2017-11-21 14:05:04 -0800782 cb->DdmPublishChunk(native ? CHUNK_TYPE("NHEN") : CHUNK_TYPE("HPEN"),
783 ArrayRef<const uint8_t>(heap_id));
Elliott Hughes767a1472011-10-26 18:49:02 -0700784}
785
Brian Carlstrom306db812014-09-05 13:01:41 -0700786void Dbg::SetAllocTrackingEnabled(bool enable) {
Man Cao8c2ff642015-05-27 17:25:30 -0700787 gc::AllocRecordObjectMap::SetAllocTrackingEnabled(enable);
Elliott Hughes545a0642011-11-08 19:10:03 -0800788}
789
Elliott Hughes545a0642011-11-08 19:10:03 -0800790class StringTable {
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700791 private:
792 struct Entry {
Andreas Gampe84eadb22017-07-07 15:08:01 -0700793 explicit Entry(const char* data_in)
794 : data(data_in), hash(ComputeModifiedUtf8Hash(data_in)), index(0) {
795 }
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700796 Entry(const Entry& entry) = default;
797 Entry(Entry&& entry) = default;
798
799 // Pointer to the actual string data.
800 const char* data;
Andreas Gampe84eadb22017-07-07 15:08:01 -0700801
802 // The hash of the data.
803 const uint32_t hash;
804
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700805 // The index. This will be filled in on Finish and is not part of the ordering, so mark it
806 // mutable.
807 mutable uint32_t index;
808
Andreas Gampe84eadb22017-07-07 15:08:01 -0700809 bool operator==(const Entry& other) const {
810 return strcmp(data, other.data) == 0;
811 }
812 };
813 struct EntryHash {
814 size_t operator()(const Entry& entry) const {
815 return entry.hash;
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700816 }
817 };
818
Elliott Hughes545a0642011-11-08 19:10:03 -0800819 public:
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700820 StringTable() : finished_(false) {
Elliott Hughes545a0642011-11-08 19:10:03 -0800821 }
822
Andreas Gampef774a4e2017-07-06 22:15:18 -0700823 void Add(const char* str, bool copy_string) {
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700824 DCHECK(!finished_);
Andreas Gampef774a4e2017-07-06 22:15:18 -0700825 if (UNLIKELY(copy_string)) {
826 // Check whether it's already there.
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700827 Entry entry(str);
828 if (table_.find(entry) != table_.end()) {
Andreas Gampef774a4e2017-07-06 22:15:18 -0700829 return;
830 }
Mathieu Chartier4345c462014-06-27 10:20:14 -0700831
Andreas Gampef774a4e2017-07-06 22:15:18 -0700832 // Make a copy.
833 size_t str_len = strlen(str);
834 char* copy = new char[str_len + 1];
835 strlcpy(copy, str, str_len + 1);
836 string_backup_.emplace_back(copy);
837 str = copy;
838 }
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700839 Entry entry(str);
840 table_.insert(entry);
841 }
842
843 // Update all entries and give them an index. Note that this is likely not the insertion order,
844 // as the set will with high likelihood reorder elements. Thus, Add must not be called after
845 // Finish, and Finish must be called before IndexOf. In that case, WriteTo will walk in
846 // the same order as Finish, and indices will agree. The order invariant, as well as indices,
847 // are enforced through debug checks.
848 void Finish() {
849 DCHECK(!finished_);
850 finished_ = true;
851 uint32_t index = 0;
852 for (auto& entry : table_) {
853 entry.index = index;
854 ++index;
855 }
Elliott Hughes545a0642011-11-08 19:10:03 -0800856 }
857
Elliott Hughesa8f93cb2012-06-08 17:08:48 -0700858 size_t IndexOf(const char* s) const {
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700859 DCHECK(finished_);
860 Entry entry(s);
861 auto it = table_.find(entry);
Elliott Hughesa8f93cb2012-06-08 17:08:48 -0700862 if (it == table_.end()) {
863 LOG(FATAL) << "IndexOf(\"" << s << "\") failed";
864 }
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700865 return it->index;
Elliott Hughes545a0642011-11-08 19:10:03 -0800866 }
867
Elliott Hughesa8f93cb2012-06-08 17:08:48 -0700868 size_t Size() const {
Elliott Hughes545a0642011-11-08 19:10:03 -0800869 return table_.size();
870 }
871
Elliott Hughesa8f93cb2012-06-08 17:08:48 -0700872 void WriteTo(std::vector<uint8_t>& bytes) const {
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700873 DCHECK(finished_);
874 uint32_t cur_index = 0;
875 for (const auto& entry : table_) {
876 DCHECK_EQ(cur_index++, entry.index);
877
878 size_t s_len = CountModifiedUtf8Chars(entry.data);
Christopher Ferris8a354052015-04-24 17:23:53 -0700879 std::unique_ptr<uint16_t[]> s_utf16(new uint16_t[s_len]);
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700880 ConvertModifiedUtf8ToUtf16(s_utf16.get(), entry.data);
Alex Lightfc588092020-01-23 15:39:08 -0800881 AppendUtf16BE(bytes, s_utf16.get(), s_len);
Elliott Hughes545a0642011-11-08 19:10:03 -0800882 }
883 }
884
885 private:
Andreas Gampe84eadb22017-07-07 15:08:01 -0700886 std::unordered_set<Entry, EntryHash> table_;
Andreas Gampef774a4e2017-07-06 22:15:18 -0700887 std::vector<std::unique_ptr<char[]>> string_backup_;
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700888
889 bool finished_;
890
Elliott Hughes545a0642011-11-08 19:10:03 -0800891 DISALLOW_COPY_AND_ASSIGN(StringTable);
892};
893
Alex Lightfc588092020-01-23 15:39:08 -0800894
Mathieu Chartiere401d142015-04-22 13:56:20 -0700895static const char* GetMethodSourceFile(ArtMethod* method)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700896 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700897 DCHECK(method != nullptr);
898 const char* source_file = method->GetDeclaringClassSourceFile();
Sebastien Hertz280286a2014-04-28 09:26:50 +0200899 return (source_file != nullptr) ? source_file : "";
900}
901
Elliott Hughes545a0642011-11-08 19:10:03 -0800902/*
903 * The data we send to DDMS contains everything we have recorded.
904 *
905 * Message header (all values big-endian):
906 * (1b) message header len (to allow future expansion); includes itself
907 * (1b) entry header len
908 * (1b) stack frame len
909 * (2b) number of entries
910 * (4b) offset to string table from start of message
911 * (2b) number of class name strings
912 * (2b) number of method name strings
913 * (2b) number of source file name strings
914 * For each entry:
915 * (4b) total allocation size
Elliott Hughes221229c2013-01-08 18:17:50 -0800916 * (2b) thread id
Elliott Hughes545a0642011-11-08 19:10:03 -0800917 * (2b) allocated object's class name index
918 * (1b) stack depth
919 * For each stack frame:
920 * (2b) method's class name
921 * (2b) method name
922 * (2b) method source file
923 * (2b) line number, clipped to 32767; -2 if native; -1 if no source
924 * (xb) class name strings
925 * (xb) method name strings
926 * (xb) source file strings
927 *
928 * As with other DDM traffic, strings are sent as a 4-byte length
929 * followed by UTF-16 data.
930 *
931 * We send up 16-bit unsigned indexes into string tables. In theory there
Brian Carlstrom306db812014-09-05 13:01:41 -0700932 * can be (kMaxAllocRecordStackDepth * alloc_record_max_) unique strings in
Elliott Hughes545a0642011-11-08 19:10:03 -0800933 * each table, but in practice there should be far fewer.
934 *
935 * The chief reason for using a string table here is to keep the size of
936 * the DDMS message to a minimum. This is partly to make the protocol
937 * efficient, but also because we have to form the whole thing up all at
938 * once in a memory buffer.
939 *
940 * We use separate string tables for class names, method names, and source
941 * files to keep the indexes small. There will generally be no overlap
942 * between the contents of these tables.
943 */
944jbyteArray Dbg::GetRecentAllocations() {
Ian Rogerscf7f1912014-10-22 22:06:39 -0700945 if ((false)) {
Elliott Hughes545a0642011-11-08 19:10:03 -0800946 DumpRecentAllocations();
947 }
948
Ian Rogers50b35e22012-10-04 10:09:15 -0700949 Thread* self = Thread::Current();
Elliott Hughes545a0642011-11-08 19:10:03 -0800950 std::vector<uint8_t> bytes;
Mathieu Chartier46e811b2013-07-10 17:09:14 -0700951 {
Brian Carlstrom306db812014-09-05 13:01:41 -0700952 MutexLock mu(self, *Locks::alloc_tracker_lock_);
Man Cao8c2ff642015-05-27 17:25:30 -0700953 gc::AllocRecordObjectMap* records = Runtime::Current()->GetHeap()->GetAllocationRecords();
954 // In case this method is called when allocation tracker is disabled,
955 // we should still send some data back.
956 gc::AllocRecordObjectMap dummy;
957 if (records == nullptr) {
958 CHECK(!Runtime::Current()->GetHeap()->IsAllocTrackingEnabled());
959 records = &dummy;
960 }
Man Cao41656de2015-07-06 18:53:15 -0700961 // We don't need to wait on the condition variable records->new_record_condition_, because this
962 // function only reads the class objects, which are already marked so it doesn't change their
963 // reachability.
Man Cao8c2ff642015-05-27 17:25:30 -0700964
Mathieu Chartier46e811b2013-07-10 17:09:14 -0700965 //
966 // Part 1: generate string tables.
967 //
968 StringTable class_names;
969 StringTable method_names;
970 StringTable filenames;
Elliott Hughes545a0642011-11-08 19:10:03 -0800971
Andreas Gampeff29cee2017-07-07 11:11:15 -0700972 VLOG(jdwp) << "Collecting StringTables.";
973
Man Cao1ed11b92015-06-11 22:47:35 -0700974 const uint16_t capped_count = CappedAllocRecordCount(records->GetRecentAllocationSize());
Brian Carlstrom306db812014-09-05 13:01:41 -0700975 uint16_t count = capped_count;
Andreas Gamped0fc7682017-07-07 14:03:08 -0700976 size_t alloc_byte_count = 0;
Man Cao8c2ff642015-05-27 17:25:30 -0700977 for (auto it = records->RBegin(), end = records->REnd();
978 count > 0 && it != end; count--, it++) {
Mathieu Chartier458b1052016-03-29 14:02:55 -0700979 const gc::AllocRecord* record = &it->second;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700980 std::string temp;
Andreas Gampef774a4e2017-07-06 22:15:18 -0700981 const char* class_descr = record->GetClassDescriptor(&temp);
982 class_names.Add(class_descr, !temp.empty());
Andreas Gamped0fc7682017-07-07 14:03:08 -0700983
984 // Size + tid + class name index + stack depth.
985 alloc_byte_count += 4u + 2u + 2u + 1u;
986
Man Cao8c2ff642015-05-27 17:25:30 -0700987 for (size_t i = 0, depth = record->GetDepth(); i < depth; i++) {
988 ArtMethod* m = record->StackElement(i).GetMethod();
Andreas Gampef774a4e2017-07-06 22:15:18 -0700989 class_names.Add(m->GetDeclaringClassDescriptor(), false);
990 method_names.Add(m->GetName(), false);
991 filenames.Add(GetMethodSourceFile(m), false);
Mathieu Chartier46e811b2013-07-10 17:09:14 -0700992 }
Andreas Gamped0fc7682017-07-07 14:03:08 -0700993
994 // Depth * (class index + method name index + file name index + line number).
995 alloc_byte_count += record->GetDepth() * (2u + 2u + 2u + 2u);
Elliott Hughes545a0642011-11-08 19:10:03 -0800996 }
997
Andreas Gampe2eeb01a2017-07-07 14:09:46 -0700998 class_names.Finish();
999 method_names.Finish();
1000 filenames.Finish();
Andreas Gampeff29cee2017-07-07 11:11:15 -07001001 VLOG(jdwp) << "Done collecting StringTables:" << std::endl
1002 << " ClassNames: " << class_names.Size() << std::endl
1003 << " MethodNames: " << method_names.Size() << std::endl
1004 << " Filenames: " << filenames.Size();
1005
Man Cao8c2ff642015-05-27 17:25:30 -07001006 LOG(INFO) << "recent allocation records: " << capped_count;
1007 LOG(INFO) << "allocation records all objects: " << records->Size();
Mathieu Chartier46e811b2013-07-10 17:09:14 -07001008
1009 //
1010 // Part 2: Generate the output and store it in the buffer.
1011 //
1012
1013 // (1b) message header len (to allow future expansion); includes itself
1014 // (1b) entry header len
1015 // (1b) stack frame len
1016 const int kMessageHeaderLen = 15;
1017 const int kEntryHeaderLen = 9;
1018 const int kStackFrameLen = 8;
Alex Lightfc588092020-01-23 15:39:08 -08001019 Append1BE(bytes, kMessageHeaderLen);
1020 Append1BE(bytes, kEntryHeaderLen);
1021 Append1BE(bytes, kStackFrameLen);
Mathieu Chartier46e811b2013-07-10 17:09:14 -07001022
1023 // (2b) number of entries
1024 // (4b) offset to string table from start of message
1025 // (2b) number of class name strings
1026 // (2b) number of method name strings
1027 // (2b) number of source file name strings
Alex Lightfc588092020-01-23 15:39:08 -08001028 Append2BE(bytes, capped_count);
Mathieu Chartier46e811b2013-07-10 17:09:14 -07001029 size_t string_table_offset = bytes.size();
Alex Lightfc588092020-01-23 15:39:08 -08001030 Append4BE(bytes, 0); // We'll patch this later...
1031 Append2BE(bytes, class_names.Size());
1032 Append2BE(bytes, method_names.Size());
1033 Append2BE(bytes, filenames.Size());
Mathieu Chartier46e811b2013-07-10 17:09:14 -07001034
Andreas Gampeff29cee2017-07-07 11:11:15 -07001035 VLOG(jdwp) << "Dumping allocations with stacks";
1036
Andreas Gamped0fc7682017-07-07 14:03:08 -07001037 // Enlarge the vector for the allocation data.
1038 size_t reserve_size = bytes.size() + alloc_byte_count;
1039 bytes.reserve(reserve_size);
1040
Ian Rogers1ff3c982014-08-12 02:30:58 -07001041 std::string temp;
Man Cao8c2ff642015-05-27 17:25:30 -07001042 count = capped_count;
1043 // The last "count" number of allocation records in "records" are the most recent "count" number
1044 // of allocations. Reverse iterate to get them. The most recent allocation is sent first.
1045 for (auto it = records->RBegin(), end = records->REnd();
1046 count > 0 && it != end; count--, it++) {
Mathieu Chartier46e811b2013-07-10 17:09:14 -07001047 // For each entry:
1048 // (4b) total allocation size
1049 // (2b) thread id
1050 // (2b) allocated object's class name index
1051 // (1b) stack depth
Mathieu Chartier458b1052016-03-29 14:02:55 -07001052 const gc::AllocRecord* record = &it->second;
Mathieu Chartier46e811b2013-07-10 17:09:14 -07001053 size_t stack_depth = record->GetDepth();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001054 size_t allocated_object_class_name_index =
Man Cao41656de2015-07-06 18:53:15 -07001055 class_names.IndexOf(record->GetClassDescriptor(&temp));
Alex Lightfc588092020-01-23 15:39:08 -08001056 Append4BE(bytes, record->ByteCount());
1057 Append2BE(bytes, static_cast<uint16_t>(record->GetTid()));
1058 Append2BE(bytes, allocated_object_class_name_index);
1059 Append1BE(bytes, stack_depth);
Mathieu Chartier46e811b2013-07-10 17:09:14 -07001060
Mathieu Chartier46e811b2013-07-10 17:09:14 -07001061 for (size_t stack_frame = 0; stack_frame < stack_depth; ++stack_frame) {
1062 // For each stack frame:
1063 // (2b) method's class name
1064 // (2b) method name
1065 // (2b) method source file
1066 // (2b) line number, clipped to 32767; -2 if native; -1 if no source
Man Cao8c2ff642015-05-27 17:25:30 -07001067 ArtMethod* m = record->StackElement(stack_frame).GetMethod();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001068 size_t class_name_index = class_names.IndexOf(m->GetDeclaringClassDescriptor());
1069 size_t method_name_index = method_names.IndexOf(m->GetName());
1070 size_t file_name_index = filenames.IndexOf(GetMethodSourceFile(m));
Alex Lightfc588092020-01-23 15:39:08 -08001071 Append2BE(bytes, class_name_index);
1072 Append2BE(bytes, method_name_index);
1073 Append2BE(bytes, file_name_index);
1074 Append2BE(bytes, record->StackElement(stack_frame).ComputeLineNumber());
Mathieu Chartier46e811b2013-07-10 17:09:14 -07001075 }
Mathieu Chartier46e811b2013-07-10 17:09:14 -07001076 }
1077
Andreas Gamped0fc7682017-07-07 14:03:08 -07001078 CHECK_EQ(bytes.size(), reserve_size);
Andreas Gampeff29cee2017-07-07 11:11:15 -07001079 VLOG(jdwp) << "Dumping tables.";
1080
Mathieu Chartier46e811b2013-07-10 17:09:14 -07001081 // (xb) class name strings
1082 // (xb) method name strings
1083 // (xb) source file strings
Alex Lightfc588092020-01-23 15:39:08 -08001084 Set4BE(&bytes[string_table_offset], bytes.size());
Mathieu Chartier46e811b2013-07-10 17:09:14 -07001085 class_names.WriteTo(bytes);
1086 method_names.WriteTo(bytes);
1087 filenames.WriteTo(bytes);
Andreas Gampeff29cee2017-07-07 11:11:15 -07001088
1089 VLOG(jdwp) << "GetRecentAllocations: data created. " << bytes.size();
Elliott Hughes545a0642011-11-08 19:10:03 -08001090 }
Ian Rogers50b35e22012-10-04 10:09:15 -07001091 JNIEnv* env = self->GetJniEnv();
Elliott Hughes545a0642011-11-08 19:10:03 -08001092 jbyteArray result = env->NewByteArray(bytes.size());
Ian Rogersc0542af2014-09-03 16:16:56 -07001093 if (result != nullptr) {
Elliott Hughes545a0642011-11-08 19:10:03 -08001094 env->SetByteArrayRegion(result, 0, bytes.size(), reinterpret_cast<const jbyte*>(&bytes[0]));
1095 }
1096 return result;
1097}
1098
Andreas Gampe04bbb5b2017-01-19 17:49:03 +00001099void Dbg::DbgThreadLifecycleCallback::ThreadStart(Thread* self) {
1100 Dbg::PostThreadStart(self);
1101}
1102
1103void Dbg::DbgThreadLifecycleCallback::ThreadDeath(Thread* self) {
1104 Dbg::PostThreadDeath(self);
1105}
1106
Elliott Hughes872d4ec2011-10-21 17:07:15 -07001107} // namespace art