blob: 027f62d8801e46176320167e6be84e947032054b [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
jeffhaoe343b762011-12-05 16:36:44 -080016
17#include "trace.h"
18
jeffhaoa9ef3fd2011-12-13 18:33:43 -080019#include <sys/uio.h>
20
Jeff Hao0abc72e2013-08-13 13:45:14 -070021#include "base/stl_util.h"
Elliott Hughes76160052012-12-12 16:31:20 -080022#include "base/unix_file/fd_file.h"
jeffhaoe343b762011-12-05 16:36:44 -080023#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080024#include "common_throws.h"
jeffhaoa9ef3fd2011-12-13 18:33:43 -080025#include "debugger.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
jeffhao725a9572012-11-13 18:20:12 -080027#include "instrumentation.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070028#include "mirror/art_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "mirror/dex_cache.h"
31#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "mirror/object-inl.h"
jeffhaoa9ef3fd2011-12-13 18:33:43 -080033#include "os.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034#include "scoped_thread_state_change.h"
Jeff Hao0abc72e2013-08-13 13:45:14 -070035#include "ScopedLocalRef.h"
jeffhaoe343b762011-12-05 16:36:44 -080036#include "thread.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070037#include "thread_list.h"
Ian Rogers166db042013-07-26 12:05:57 -070038#if !defined(ART_USE_PORTABLE_COMPILER)
39#include "entrypoints/quick/quick_entrypoints.h"
40#endif
jeffhao2692b572011-12-16 15:42:28 -080041
42namespace art {
43
Elliott Hughese119a362012-05-22 17:37:06 -070044// File format:
45// header
46// record 0
47// record 1
48// ...
49//
50// Header format:
51// u4 magic ('SLOW')
52// u2 version
53// u2 offset to data
54// u8 start date/time in usec
55// u2 record size in bytes (version >= 2 only)
56// ... padding to 32 bytes
57//
58// Record format v1:
59// u1 thread ID
60// u4 method ID | method action
61// u4 time delta since start, in usec
62//
63// Record format v2:
64// u2 thread ID
65// u4 method ID | method action
66// u4 time delta since start, in usec
67//
68// Record format v3:
69// u2 thread ID
70// u4 method ID | method action
71// u4 time delta since start, in usec
72// u4 wall time since start, in usec (when clock == "dual" only)
73//
74// 32 bits of microseconds is 70 minutes.
75//
76// All values are stored in little-endian order.
77
Ian Rogers62d6c772013-02-27 08:32:07 -080078enum TraceAction {
Brian Carlstrom7934ac22013-07-26 10:54:15 -070079 kTraceMethodEnter = 0x00, // method entry
80 kTraceMethodExit = 0x01, // method exit
81 kTraceUnroll = 0x02, // method exited by exception unrolling
Ian Rogers62d6c772013-02-27 08:32:07 -080082 // 0x03 currently unused
Brian Carlstrom7934ac22013-07-26 10:54:15 -070083 kTraceMethodActionMask = 0x03, // two bits
Ian Rogers62d6c772013-02-27 08:32:07 -080084};
85
Jeff Hao0abc72e2013-08-13 13:45:14 -070086class BuildStackTraceVisitor : public StackVisitor {
87 public:
88 explicit BuildStackTraceVisitor(Thread* thread) : StackVisitor(thread, NULL),
Jeff Hao5ce4b172013-08-16 16:27:18 -070089 method_trace_(Trace::AllocStackTrace()) {}
Jeff Hao0abc72e2013-08-13 13:45:14 -070090
Ian Rogersef7d42f2014-01-06 12:55:46 -080091 bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogerse2f77e72013-08-13 19:19:40 -070092 mirror::ArtMethod* m = GetMethod();
Jeff Hao0abc72e2013-08-13 13:45:14 -070093 // Ignore runtime frames (in particular callee save).
94 if (!m->IsRuntimeMethod()) {
95 method_trace_->push_back(m);
96 }
97 return true;
98 }
99
100 // Returns a stack trace where the topmost frame corresponds with the first element of the vector.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700101 std::vector<mirror::ArtMethod*>* GetStackTrace() const {
Jeff Hao0abc72e2013-08-13 13:45:14 -0700102 return method_trace_;
103 }
104
105 private:
Ian Rogerse2f77e72013-08-13 19:19:40 -0700106 std::vector<mirror::ArtMethod*>* const method_trace_;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700107};
108
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800109static const char kTraceTokenChar = '*';
110static const uint16_t kTraceHeaderLength = 32;
111static const uint32_t kTraceMagicValue = 0x574f4c53;
112static const uint16_t kTraceVersionSingleClock = 2;
113static const uint16_t kTraceVersionDualClock = 3;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700114static const uint16_t kTraceRecordSizeSingleClock = 10; // using v2
115static const uint16_t kTraceRecordSizeDualClock = 14; // using v3 with two timestamps
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800116
Ian Rogerse63db272014-07-15 15:36:11 -0700117TraceClockSource Trace::default_clock_source_ = kDefaultTraceClockSource;
Elliott Hughese119a362012-05-22 17:37:06 -0700118
Jeff Hao0abc72e2013-08-13 13:45:14 -0700119Trace* volatile Trace::the_trace_ = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700120pthread_t Trace::sampling_pthread_ = 0U;
Ian Rogers700a4022014-05-19 16:49:03 -0700121std::unique_ptr<std::vector<mirror::ArtMethod*>> Trace::temp_stack_trace_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800122
Brian Carlstromea46f952013-07-30 01:26:50 -0700123static mirror::ArtMethod* DecodeTraceMethodId(uint32_t tmid) {
124 return reinterpret_cast<mirror::ArtMethod*>(tmid & ~kTraceMethodActionMask);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800125}
Elliott Hughese119a362012-05-22 17:37:06 -0700126
Ian Rogers62d6c772013-02-27 08:32:07 -0800127static TraceAction DecodeTraceAction(uint32_t tmid) {
128 return static_cast<TraceAction>(tmid & kTraceMethodActionMask);
129}
130
Ian Rogersef7d42f2014-01-06 12:55:46 -0800131static uint32_t EncodeTraceMethodAndAction(mirror::ArtMethod* method,
Ian Rogers62d6c772013-02-27 08:32:07 -0800132 TraceAction action) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133 uint32_t tmid = PointerToLowMemUInt32(method) | action;
Ian Rogers62d6c772013-02-27 08:32:07 -0800134 DCHECK_EQ(method, DecodeTraceMethodId(tmid));
135 return tmid;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800136}
137
Jeff Hao5ce4b172013-08-16 16:27:18 -0700138std::vector<mirror::ArtMethod*>* Trace::AllocStackTrace() {
139 if (temp_stack_trace_.get() != NULL) {
140 return temp_stack_trace_.release();
141 } else {
142 return new std::vector<mirror::ArtMethod*>();
143 }
144}
145
146void Trace::FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace) {
147 stack_trace->clear();
148 temp_stack_trace_.reset(stack_trace);
149}
150
Ian Rogerse63db272014-07-15 15:36:11 -0700151void Trace::SetDefaultClockSource(TraceClockSource clock_source) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800152#if defined(HAVE_POSIX_CLOCKS)
153 default_clock_source_ = clock_source;
154#else
Ian Rogerse63db272014-07-15 15:36:11 -0700155 if (clock_source != kTraceClockSourceWall) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700156 LOG(WARNING) << "Ignoring tracing request to use CPU time.";
Ian Rogers62d6c772013-02-27 08:32:07 -0800157 }
158#endif
159}
160
Ian Rogerse63db272014-07-15 15:36:11 -0700161static uint16_t GetTraceVersion(TraceClockSource clock_source) {
162 return (clock_source == kTraceClockSourceDual) ? kTraceVersionDualClock
Ian Rogers62d6c772013-02-27 08:32:07 -0800163 : kTraceVersionSingleClock;
164}
165
Ian Rogerse63db272014-07-15 15:36:11 -0700166static uint16_t GetRecordSize(TraceClockSource clock_source) {
167 return (clock_source == kTraceClockSourceDual) ? kTraceRecordSizeDualClock
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 : kTraceRecordSizeSingleClock;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800169}
170
Elliott Hughese119a362012-05-22 17:37:06 -0700171bool Trace::UseThreadCpuClock() {
Ian Rogerse63db272014-07-15 15:36:11 -0700172 return (clock_source_ == kTraceClockSourceThreadCpu) ||
173 (clock_source_ == kTraceClockSourceDual);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800174}
175
Elliott Hughese119a362012-05-22 17:37:06 -0700176bool Trace::UseWallClock() {
Ian Rogerse63db272014-07-15 15:36:11 -0700177 return (clock_source_ == kTraceClockSourceWall) ||
178 (clock_source_ == kTraceClockSourceDual);
Elliott Hughese119a362012-05-22 17:37:06 -0700179}
180
Jeff Haoc5d824a2014-07-28 18:35:38 -0700181void Trace::MeasureClockOverhead() {
182 if (UseThreadCpuClock()) {
Jeff Hao57dac6e2013-08-15 16:36:24 -0700183 Thread::Current()->GetCpuMicroTime();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800184 }
Jeff Haoc5d824a2014-07-28 18:35:38 -0700185 if (UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800186 MicroTime();
187 }
188}
189
Jeff Hao5ce4b172013-08-16 16:27:18 -0700190// Compute an average time taken to measure clocks.
Jeff Haoc5d824a2014-07-28 18:35:38 -0700191uint32_t Trace::GetClockOverheadNanoSeconds() {
Jeff Hao57dac6e2013-08-15 16:36:24 -0700192 Thread* self = Thread::Current();
193 uint64_t start = self->GetCpuMicroTime();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800194
195 for (int i = 4000; i > 0; i--) {
Jeff Haoc5d824a2014-07-28 18:35:38 -0700196 MeasureClockOverhead();
197 MeasureClockOverhead();
198 MeasureClockOverhead();
199 MeasureClockOverhead();
200 MeasureClockOverhead();
201 MeasureClockOverhead();
202 MeasureClockOverhead();
203 MeasureClockOverhead();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800204 }
205
Jeff Hao5ce4b172013-08-16 16:27:18 -0700206 uint64_t elapsed_us = self->GetCpuMicroTime() - start;
207 return static_cast<uint32_t>(elapsed_us / 32);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800208}
209
Elliott Hughesffb465f2012-03-01 18:46:05 -0800210// TODO: put this somewhere with the big-endian equivalent used by JDWP.
211static void Append2LE(uint8_t* buf, uint16_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700212 *buf++ = static_cast<uint8_t>(val);
213 *buf++ = static_cast<uint8_t>(val >> 8);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800214}
215
Elliott Hughesffb465f2012-03-01 18:46:05 -0800216// TODO: put this somewhere with the big-endian equivalent used by JDWP.
217static void Append4LE(uint8_t* buf, uint32_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700218 *buf++ = static_cast<uint8_t>(val);
219 *buf++ = static_cast<uint8_t>(val >> 8);
220 *buf++ = static_cast<uint8_t>(val >> 16);
221 *buf++ = static_cast<uint8_t>(val >> 24);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800222}
223
Elliott Hughesffb465f2012-03-01 18:46:05 -0800224// TODO: put this somewhere with the big-endian equivalent used by JDWP.
225static void Append8LE(uint8_t* buf, uint64_t val) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700226 *buf++ = static_cast<uint8_t>(val);
227 *buf++ = static_cast<uint8_t>(val >> 8);
228 *buf++ = static_cast<uint8_t>(val >> 16);
229 *buf++ = static_cast<uint8_t>(val >> 24);
230 *buf++ = static_cast<uint8_t>(val >> 32);
231 *buf++ = static_cast<uint8_t>(val >> 40);
232 *buf++ = static_cast<uint8_t>(val >> 48);
233 *buf++ = static_cast<uint8_t>(val >> 56);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800234}
235
Jeff Hao0abc72e2013-08-13 13:45:14 -0700236static void GetSample(Thread* thread, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
237 BuildStackTraceVisitor build_trace_visitor(thread);
238 build_trace_visitor.WalkStack();
Ian Rogerse2f77e72013-08-13 19:19:40 -0700239 std::vector<mirror::ArtMethod*>* stack_trace = build_trace_visitor.GetStackTrace();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700240 Trace* the_trace = reinterpret_cast<Trace*>(arg);
241 the_trace->CompareAndUpdateStackTrace(thread, stack_trace);
242}
243
Jeff Hao5ce4b172013-08-16 16:27:18 -0700244static void ClearThreadStackTraceAndClockBase(Thread* thread, void* arg) {
245 thread->SetTraceClockBase(0);
246 std::vector<mirror::ArtMethod*>* stack_trace = thread->GetStackTraceSample();
247 thread->SetStackTraceSample(NULL);
248 delete stack_trace;
249}
250
Jeff Hao0abc72e2013-08-13 13:45:14 -0700251void Trace::CompareAndUpdateStackTrace(Thread* thread,
Ian Rogerse2f77e72013-08-13 19:19:40 -0700252 std::vector<mirror::ArtMethod*>* stack_trace) {
Jeff Hao0abc72e2013-08-13 13:45:14 -0700253 CHECK_EQ(pthread_self(), sampling_pthread_);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700254 std::vector<mirror::ArtMethod*>* old_stack_trace = thread->GetStackTraceSample();
255 // Update the thread's stack trace sample.
256 thread->SetStackTraceSample(stack_trace);
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700257 // Read timer clocks to use for all events in this trace.
258 uint32_t thread_clock_diff = 0;
259 uint32_t wall_clock_diff = 0;
260 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700261 if (old_stack_trace == NULL) {
262 // If there's no previous stack trace sample for this thread, log an entry event for all
Jeff Hao0abc72e2013-08-13 13:45:14 -0700263 // methods in the trace.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700264 for (std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700265 rit != stack_trace->rend(); ++rit) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700266 LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered,
267 thread_clock_diff, wall_clock_diff);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700268 }
269 } else {
270 // If there's a previous stack trace for this thread, diff the traces and emit entry and exit
271 // events accordingly.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700272 std::vector<mirror::ArtMethod*>::reverse_iterator old_rit = old_stack_trace->rbegin();
273 std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700274 // Iterate bottom-up over both traces until there's a difference between them.
275 while (old_rit != old_stack_trace->rend() && rit != stack_trace->rend() && *old_rit == *rit) {
276 old_rit++;
277 rit++;
278 }
279 // Iterate top-down over the old trace until the point where they differ, emitting exit events.
Ian Rogerse2f77e72013-08-13 19:19:40 -0700280 for (std::vector<mirror::ArtMethod*>::iterator old_it = old_stack_trace->begin();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700281 old_it != old_rit.base(); ++old_it) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700282 LogMethodTraceEvent(thread, *old_it, instrumentation::Instrumentation::kMethodExited,
283 thread_clock_diff, wall_clock_diff);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700284 }
285 // Iterate bottom-up over the new trace from the point where they differ, emitting entry events.
286 for (; rit != stack_trace->rend(); ++rit) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700287 LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered,
288 thread_clock_diff, wall_clock_diff);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700289 }
Jeff Hao5ce4b172013-08-16 16:27:18 -0700290 FreeStackTrace(old_stack_trace);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700291 }
292}
293
294void* Trace::RunSamplingThread(void* arg) {
295 Runtime* runtime = Runtime::Current();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800296 intptr_t interval_us = reinterpret_cast<intptr_t>(arg);
Jeff Hao4044bda2014-01-06 15:50:45 -0800297 CHECK_GE(interval_us, 0);
Jeff Hao0abc72e2013-08-13 13:45:14 -0700298 CHECK(runtime->AttachCurrentThread("Sampling Profiler", true, runtime->GetSystemThreadGroup(),
299 !runtime->IsCompiler()));
300
301 while (true) {
Jeff Hao23009dc2013-08-22 15:36:42 -0700302 usleep(interval_us);
Jeff Hao5ce4b172013-08-16 16:27:18 -0700303 ATRACE_BEGIN("Profile sampling");
Jeff Hao0abc72e2013-08-13 13:45:14 -0700304 Thread* self = Thread::Current();
305 Trace* the_trace;
306 {
307 MutexLock mu(self, *Locks::trace_lock_);
308 the_trace = the_trace_;
309 if (the_trace == NULL) {
310 break;
311 }
312 }
313
314 runtime->GetThreadList()->SuspendAll();
315 {
316 MutexLock mu(self, *Locks::thread_list_lock_);
317 runtime->GetThreadList()->ForEach(GetSample, the_trace);
318 }
319 runtime->GetThreadList()->ResumeAll();
Jeff Hao5ce4b172013-08-16 16:27:18 -0700320 ATRACE_END();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700321 }
322
323 runtime->DetachCurrentThread();
324 return NULL;
325}
326
Ian Rogers62d6c772013-02-27 08:32:07 -0800327void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags,
Jeff Hao23009dc2013-08-22 15:36:42 -0700328 bool direct_to_ddms, bool sampling_enabled, int interval_us) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800329 Thread* self = Thread::Current();
330 {
331 MutexLock mu(self, *Locks::trace_lock_);
332 if (the_trace_ != NULL) {
333 LOG(ERROR) << "Trace already in progress, ignoring this request";
334 return;
335 }
jeffhaoe343b762011-12-05 16:36:44 -0800336 }
Jeff Haod063d912014-09-08 09:38:18 -0700337
338 // Check interval if sampling is enabled
339 if (sampling_enabled && interval_us <= 0) {
340 LOG(ERROR) << "Invalid sampling interval: " << interval_us;
341 ScopedObjectAccess soa(self);
342 ThrowRuntimeException("Invalid sampling interval: %d", interval_us);
343 return;
344 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800345
jeffhao2692b572011-12-16 15:42:28 -0800346 // Open trace file if not going directly to ddms.
Ian Rogers700a4022014-05-19 16:49:03 -0700347 std::unique_ptr<File> trace_file;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800348 if (!direct_to_ddms) {
349 if (trace_fd < 0) {
Brian Carlstrom7571e8b2013-08-12 17:04:14 -0700350 trace_file.reset(OS::CreateEmptyFile(trace_filename));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800351 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -0800352 trace_file.reset(new File(trace_fd, "tracefile"));
Elliott Hughes76160052012-12-12 16:31:20 -0800353 trace_file->DisableAutoClose();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800354 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800355 if (trace_file.get() == NULL) {
jeffhaob5e81852012-03-12 11:15:45 -0700356 PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800357 ScopedObjectAccess soa(self);
358 ThrowRuntimeException("Unable to open trace file '%s'", trace_filename);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800359 return;
360 }
361 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800362
Jeff Haod063d912014-09-08 09:38:18 -0700363 Runtime* runtime = Runtime::Current();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700364
365 // Enable count of allocs if specified in the flags.
366 bool enable_stats = false;
367
Jeff Haod063d912014-09-08 09:38:18 -0700368 runtime->GetThreadList()->SuspendAll();
369
jeffhao2692b572011-12-16 15:42:28 -0800370 // Create Trace object.
Ian Rogers62d6c772013-02-27 08:32:07 -0800371 {
372 MutexLock mu(self, *Locks::trace_lock_);
Brian Carlstromdf629502013-07-17 22:39:56 -0700373 if (the_trace_ != NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800374 LOG(ERROR) << "Trace already in progress, ignoring this request";
375 } else {
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700376 enable_stats = (flags && kTraceCountAllocs) != 0;
Jeff Hao23009dc2013-08-22 15:36:42 -0700377 the_trace_ = new Trace(trace_file.release(), buffer_size, flags, sampling_enabled);
Jeff Hao23009dc2013-08-22 15:36:42 -0700378 if (sampling_enabled) {
379 CHECK_PTHREAD_CALL(pthread_create, (&sampling_pthread_, NULL, &RunSamplingThread,
380 reinterpret_cast<void*>(interval_us)),
381 "Sampling profiler thread");
Jeff Hao0abc72e2013-08-13 13:45:14 -0700382 } else {
383 runtime->GetInstrumentation()->AddListener(the_trace_,
384 instrumentation::Instrumentation::kMethodEntered |
385 instrumentation::Instrumentation::kMethodExited |
386 instrumentation::Instrumentation::kMethodUnwind);
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100387 runtime->GetInstrumentation()->EnableMethodTracing();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700388 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800389 }
jeffhao0791adc2012-04-04 11:14:32 -0700390 }
Jeff Haod063d912014-09-08 09:38:18 -0700391
Ian Rogers62d6c772013-02-27 08:32:07 -0800392 runtime->GetThreadList()->ResumeAll();
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700393
394 // Can't call this when holding the mutator lock.
395 if (enable_stats) {
396 runtime->SetStatsEnabled(true);
397 }
jeffhao2692b572011-12-16 15:42:28 -0800398}
399
400void Trace::Stop() {
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700401 bool stop_alloc_counting = false;
Ian Rogers62d6c772013-02-27 08:32:07 -0800402 Runtime* runtime = Runtime::Current();
403 runtime->GetThreadList()->SuspendAll();
404 Trace* the_trace = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700405 pthread_t sampling_pthread = 0U;
Ian Rogers62d6c772013-02-27 08:32:07 -0800406 {
407 MutexLock mu(Thread::Current(), *Locks::trace_lock_);
408 if (the_trace_ == NULL) {
409 LOG(ERROR) << "Trace stop requested, but no trace currently running";
410 } else {
411 the_trace = the_trace_;
412 the_trace_ = NULL;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700413 sampling_pthread = sampling_pthread_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800414 }
jeffhao2692b572011-12-16 15:42:28 -0800415 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800416 if (the_trace != NULL) {
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700417 stop_alloc_counting = (the_trace->flags_ & kTraceCountAllocs) != 0;
Ian Rogers62d6c772013-02-27 08:32:07 -0800418 the_trace->FinishTracing();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700419
Jeff Hao23009dc2013-08-22 15:36:42 -0700420 if (the_trace->sampling_enabled_) {
Jeff Hao5ce4b172013-08-16 16:27:18 -0700421 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
422 runtime->GetThreadList()->ForEach(ClearThreadStackTraceAndClockBase, NULL);
423 } else {
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100424 runtime->GetInstrumentation()->DisableMethodTracing();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700425 runtime->GetInstrumentation()->RemoveListener(the_trace,
426 instrumentation::Instrumentation::kMethodEntered |
427 instrumentation::Instrumentation::kMethodExited |
428 instrumentation::Instrumentation::kMethodUnwind);
429 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800430 delete the_trace;
431 }
432 runtime->GetThreadList()->ResumeAll();
Jeff Hao0abc72e2013-08-13 13:45:14 -0700433
Mathieu Chartier9ef78b52014-09-25 17:03:12 -0700434 if (stop_alloc_counting) {
435 // Can be racy since SetStatsEnabled is not guarded by any locks.
436 Runtime::Current()->SetStatsEnabled(false);
437 }
438
Jeff Hao23009dc2013-08-22 15:36:42 -0700439 if (sampling_pthread != 0U) {
Jeff Hao0abc72e2013-08-13 13:45:14 -0700440 CHECK_PTHREAD_CALL(pthread_join, (sampling_pthread, NULL), "sampling thread shutdown");
Jeff Haod063d912014-09-08 09:38:18 -0700441 sampling_pthread_ = 0U;
Jeff Hao0abc72e2013-08-13 13:45:14 -0700442 }
jeffhao2692b572011-12-16 15:42:28 -0800443}
444
jeffhaob5e81852012-03-12 11:15:45 -0700445void Trace::Shutdown() {
Jeff Hao64caa7d2013-08-29 11:18:01 -0700446 if (GetMethodTracingMode() != kTracingInactive) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800447 Stop();
jeffhaob5e81852012-03-12 11:15:45 -0700448 }
jeffhaob5e81852012-03-12 11:15:45 -0700449}
450
Jeff Hao64caa7d2013-08-29 11:18:01 -0700451TracingMode Trace::GetMethodTracingMode() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800452 MutexLock mu(Thread::Current(), *Locks::trace_lock_);
Jeff Hao64caa7d2013-08-29 11:18:01 -0700453 if (the_trace_ == NULL) {
454 return kTracingInactive;
455 } else if (the_trace_->sampling_enabled_) {
456 return kSampleProfilingActive;
457 } else {
458 return kMethodTracingActive;
459 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800460}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800461
Jeff Hao23009dc2013-08-22 15:36:42 -0700462Trace::Trace(File* trace_file, int buffer_size, int flags, bool sampling_enabled)
Ian Rogers62d6c772013-02-27 08:32:07 -0800463 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
Jeff Hao23009dc2013-08-22 15:36:42 -0700464 sampling_enabled_(sampling_enabled), clock_source_(default_clock_source_),
Jeff Haoc5d824a2014-07-28 18:35:38 -0700465 buffer_size_(buffer_size), start_time_(MicroTime()),
466 clock_overhead_ns_(GetClockOverheadNanoSeconds()), cur_offset_(0), overflow_(false) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800467 // Set up the beginning of the trace.
Ian Rogers62d6c772013-02-27 08:32:07 -0800468 uint16_t trace_version = GetTraceVersion(clock_source_);
jeffhao2692b572011-12-16 15:42:28 -0800469 memset(buf_.get(), 0, kTraceHeaderLength);
470 Append4LE(buf_.get(), kTraceMagicValue);
Ian Rogers62d6c772013-02-27 08:32:07 -0800471 Append2LE(buf_.get() + 4, trace_version);
jeffhao2692b572011-12-16 15:42:28 -0800472 Append2LE(buf_.get() + 6, kTraceHeaderLength);
473 Append8LE(buf_.get() + 8, start_time_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800474 if (trace_version >= kTraceVersionDualClock) {
475 uint16_t record_size = GetRecordSize(clock_source_);
476 Append2LE(buf_.get() + 16, record_size);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800477 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800478
jeffhao2692b572011-12-16 15:42:28 -0800479 // Update current offset.
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700480 cur_offset_.StoreRelaxed(kTraceHeaderLength);
Ian Rogers62d6c772013-02-27 08:32:07 -0800481}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800482
Ian Rogerse63db272014-07-15 15:36:11 -0700483static void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source)
Ian Rogers62d6c772013-02-27 08:32:07 -0800484 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
485 uint8_t* ptr = buf + kTraceHeaderLength;
486 uint8_t* end = buf + buf_size;
487
488 while (ptr < end) {
489 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Brian Carlstromea46f952013-07-30 01:26:50 -0700490 mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800491 TraceAction action = DecodeTraceAction(tmid);
492 LOG(INFO) << PrettyMethod(method) << " " << static_cast<int>(action);
493 ptr += GetRecordSize(clock_source);
494 }
jeffhaoe343b762011-12-05 16:36:44 -0800495}
496
jeffhao2692b572011-12-16 15:42:28 -0800497void Trace::FinishTracing() {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800498 // Compute elapsed time.
499 uint64_t elapsed = MicroTime() - start_time_;
500
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700501 size_t final_offset = cur_offset_.LoadRelaxed();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800502
Brian Carlstromea46f952013-07-30 01:26:50 -0700503 std::set<mirror::ArtMethod*> visited_methods;
Ian Rogers62d6c772013-02-27 08:32:07 -0800504 GetVisitedMethods(final_offset, &visited_methods);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800505
506 std::ostringstream os;
507
508 os << StringPrintf("%cversion\n", kTraceTokenChar);
Ian Rogers62d6c772013-02-27 08:32:07 -0800509 os << StringPrintf("%d\n", GetTraceVersion(clock_source_));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800510 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
511 if (UseThreadCpuClock()) {
512 if (UseWallClock()) {
513 os << StringPrintf("clock=dual\n");
514 } else {
515 os << StringPrintf("clock=thread-cpu\n");
516 }
517 } else {
518 os << StringPrintf("clock=wall\n");
519 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800520 os << StringPrintf("elapsed-time-usec=%" PRIu64 "\n", elapsed);
Ian Rogers62d6c772013-02-27 08:32:07 -0800521 size_t num_records = (final_offset - kTraceHeaderLength) / GetRecordSize(clock_source_);
522 os << StringPrintf("num-method-calls=%zd\n", num_records);
Jeff Haoc5d824a2014-07-28 18:35:38 -0700523 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead_ns_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800524 os << StringPrintf("vm=art\n");
jeffhao0791adc2012-04-04 11:14:32 -0700525 if ((flags_ & kTraceCountAllocs) != 0) {
526 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
527 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
528 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
529 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800530 os << StringPrintf("%cthreads\n", kTraceTokenChar);
531 DumpThreadList(os);
532 os << StringPrintf("%cmethods\n", kTraceTokenChar);
Ian Rogers62d6c772013-02-27 08:32:07 -0800533 DumpMethodList(os, visited_methods);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800534 os << StringPrintf("%cend\n", kTraceTokenChar);
535
536 std::string header(os.str());
jeffhao2692b572011-12-16 15:42:28 -0800537 if (trace_file_.get() == NULL) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700538 iovec iov[2];
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800539 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
540 iov[0].iov_len = header.length();
jeffhao2692b572011-12-16 15:42:28 -0800541 iov[1].iov_base = buf_.get();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800542 iov[1].iov_len = final_offset;
543 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
Ian Rogers62d6c772013-02-27 08:32:07 -0800544 const bool kDumpTraceInfo = false;
545 if (kDumpTraceInfo) {
546 LOG(INFO) << "Trace sent:\n" << header;
547 DumpBuf(buf_.get(), final_offset, clock_source_);
548 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800549 } else {
550 if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
jeffhao2692b572011-12-16 15:42:28 -0800551 !trace_file_->WriteFully(buf_.get(), final_offset)) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700552 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
553 PLOG(ERROR) << detail;
Ian Rogers62d6c772013-02-27 08:32:07 -0800554 ThrowRuntimeException("%s", detail.c_str());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800555 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800556 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800557}
558
Ian Rogers62d6c772013-02-27 08:32:07 -0800559void Trace::DexPcMoved(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800560 mirror::ArtMethod* method, uint32_t new_dex_pc) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800561 // We're not recorded to listen to this kind of event, so complain.
562 LOG(ERROR) << "Unexpected dex PC event in tracing " << PrettyMethod(method) << " " << new_dex_pc;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700563}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800564
Sebastien Hertz3f52eaf2014-04-04 17:50:18 +0200565void Trace::FieldRead(Thread* /*thread*/, mirror::Object* this_object,
566 mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field)
567 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
568 // We're not recorded to listen to this kind of event, so complain.
569 LOG(ERROR) << "Unexpected field read event in tracing " << PrettyMethod(method) << " " << dex_pc;
570}
571
572void Trace::FieldWritten(Thread* /*thread*/, mirror::Object* this_object,
573 mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field,
574 const JValue& field_value)
575 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
576 // We're not recorded to listen to this kind of event, so complain.
577 LOG(ERROR) << "Unexpected field write event in tracing " << PrettyMethod(method) << " " << dex_pc;
578}
579
Ian Rogers62d6c772013-02-27 08:32:07 -0800580void Trace::MethodEntered(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800581 mirror::ArtMethod* method, uint32_t dex_pc) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700582 uint32_t thread_clock_diff = 0;
583 uint32_t wall_clock_diff = 0;
584 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
585 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodEntered,
586 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800587}
588
589void Trace::MethodExited(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800590 mirror::ArtMethod* method, uint32_t dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800591 const JValue& return_value) {
592 UNUSED(return_value);
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700593 uint32_t thread_clock_diff = 0;
594 uint32_t wall_clock_diff = 0;
595 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
596 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodExited,
597 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800598}
599
Sebastien Hertz51db44a2013-11-19 10:00:29 +0100600void Trace::MethodUnwind(Thread* thread, mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800601 mirror::ArtMethod* method, uint32_t dex_pc) {
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700602 uint32_t thread_clock_diff = 0;
603 uint32_t wall_clock_diff = 0;
604 ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
605 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodUnwind,
606 thread_clock_diff, wall_clock_diff);
Ian Rogers62d6c772013-02-27 08:32:07 -0800607}
608
609void Trace::ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
Brian Carlstromea46f952013-07-30 01:26:50 -0700610 mirror::ArtMethod* catch_method, uint32_t catch_dex_pc,
Ian Rogers62d6c772013-02-27 08:32:07 -0800611 mirror::Throwable* exception_object)
612 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
613 LOG(ERROR) << "Unexpected exception caught event in tracing";
614}
615
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700616void Trace::ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff) {
617 if (UseThreadCpuClock()) {
618 uint64_t clock_base = thread->GetTraceClockBase();
619 if (UNLIKELY(clock_base == 0)) {
620 // First event, record the base time in the map.
621 uint64_t time = thread->GetCpuMicroTime();
622 thread->SetTraceClockBase(time);
623 } else {
624 *thread_clock_diff = thread->GetCpuMicroTime() - clock_base;
625 }
626 }
627 if (UseWallClock()) {
628 *wall_clock_diff = MicroTime() - start_time_;
629 }
630}
631
Ian Rogersef7d42f2014-01-06 12:55:46 -0800632void Trace::LogMethodTraceEvent(Thread* thread, mirror::ArtMethod* method,
Jeff Haoc1ff4b72013-08-19 11:33:10 -0700633 instrumentation::Instrumentation::InstrumentationEvent event,
634 uint32_t thread_clock_diff, uint32_t wall_clock_diff) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800635 // Advance cur_offset_ atomically.
636 int32_t new_offset;
637 int32_t old_offset;
638 do {
Ian Rogers8ab25ef2014-07-09 18:00:50 -0700639 old_offset = cur_offset_.LoadRelaxed();
Ian Rogers62d6c772013-02-27 08:32:07 -0800640 new_offset = old_offset + GetRecordSize(clock_source_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800641 if (new_offset > buffer_size_) {
642 overflow_ = true;
643 return;
644 }
Ian Rogers54fb8fd2014-07-09 23:16:06 -0700645 } while (!cur_offset_.CompareExchangeWeakSequentiallyConsistent(old_offset, new_offset));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800646
Ian Rogers62d6c772013-02-27 08:32:07 -0800647 TraceAction action = kTraceMethodEnter;
648 switch (event) {
649 case instrumentation::Instrumentation::kMethodEntered:
650 action = kTraceMethodEnter;
651 break;
652 case instrumentation::Instrumentation::kMethodExited:
653 action = kTraceMethodExit;
654 break;
655 case instrumentation::Instrumentation::kMethodUnwind:
656 action = kTraceUnroll;
657 break;
658 default:
659 UNIMPLEMENTED(FATAL) << "Unexpected event: " << event;
660 }
661
662 uint32_t method_value = EncodeTraceMethodAndAction(method, action);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800663
664 // Write data
jeffhao2692b572011-12-16 15:42:28 -0800665 uint8_t* ptr = buf_.get() + old_offset;
Ian Rogers62d6c772013-02-27 08:32:07 -0800666 Append2LE(ptr, thread->GetTid());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800667 Append4LE(ptr + 2, method_value);
668 ptr += 6;
669
670 if (UseThreadCpuClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800671 Append4LE(ptr, thread_clock_diff);
672 ptr += 4;
673 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800674 if (UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800675 Append4LE(ptr, wall_clock_diff);
676 }
677}
678
Ian Rogers62d6c772013-02-27 08:32:07 -0800679void Trace::GetVisitedMethods(size_t buf_size,
Brian Carlstromea46f952013-07-30 01:26:50 -0700680 std::set<mirror::ArtMethod*>* visited_methods) {
jeffhao2692b572011-12-16 15:42:28 -0800681 uint8_t* ptr = buf_.get() + kTraceHeaderLength;
Ian Rogers62d6c772013-02-27 08:32:07 -0800682 uint8_t* end = buf_.get() + buf_size;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800683
684 while (ptr < end) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800685 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
Brian Carlstromea46f952013-07-30 01:26:50 -0700686 mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
Ian Rogers62d6c772013-02-27 08:32:07 -0800687 visited_methods->insert(method);
688 ptr += GetRecordSize(clock_source_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800689 }
690}
691
Mathieu Chartier02e25112013-08-14 16:14:24 -0700692void Trace::DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods) {
Mathieu Chartier02e25112013-08-14 16:14:24 -0700693 for (const auto& method : visited_methods) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700694 os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700695 PrettyDescriptor(method->GetDeclaringClassDescriptor()).c_str(), method->GetName(),
696 method->GetSignature().ToString().c_str(), method->GetDeclaringClassSourceFile());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800697 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800698}
699
700static void DumpThread(Thread* t, void* arg) {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800701 std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
702 std::string name;
703 t->GetThreadName(name);
704 os << t->GetTid() << "\t" << name << "\n";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800705}
706
707void Trace::DumpThreadList(std::ostream& os) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700708 Thread* self = Thread::Current();
709 Locks::thread_list_lock_->AssertNotHeld(self);
710 MutexLock mu(self, *Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800711 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
jeffhaoe343b762011-12-05 16:36:44 -0800712}
713
jeffhaoe343b762011-12-05 16:36:44 -0800714} // namespace art