blob: 32932907e598573d4173c433b3a8146acc3945a0 [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
Elliott Hughes76160052012-12-12 16:31:20 -080021#include "base/unix_file/fd_file.h"
jeffhaoe343b762011-12-05 16:36:44 -080022#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080023#include "common_throws.h"
jeffhaoa9ef3fd2011-12-13 18:33:43 -080024#include "debugger.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
jeffhao725a9572012-11-13 18:20:12 -080026#include "instrumentation.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/abstract_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/dex_cache.h"
30#include "mirror/object_array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "mirror/object-inl.h"
Ian Rogersc928de92013-02-27 14:30:44 -080032#if !defined(ART_USE_PORTABLE_COMPILER)
Ian Rogers57b86d42012-03-27 16:05:41 -070033#include "oat/runtime/oat_support_entrypoints.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070034#endif
jeffhaoa9ef3fd2011-12-13 18:33:43 -080035#include "object_utils.h"
36#include "os.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070037#include "scoped_thread_state_change.h"
jeffhaoe343b762011-12-05 16:36:44 -080038#include "thread.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070039#include "thread_list.h"
jeffhao2692b572011-12-16 15:42:28 -080040
41namespace art {
42
Elliott Hughese119a362012-05-22 17:37:06 -070043// File format:
44// header
45// record 0
46// record 1
47// ...
48//
49// Header format:
50// u4 magic ('SLOW')
51// u2 version
52// u2 offset to data
53// u8 start date/time in usec
54// u2 record size in bytes (version >= 2 only)
55// ... padding to 32 bytes
56//
57// Record format v1:
58// u1 thread ID
59// u4 method ID | method action
60// u4 time delta since start, in usec
61//
62// Record format v2:
63// u2 thread ID
64// u4 method ID | method action
65// u4 time delta since start, in usec
66//
67// Record format v3:
68// u2 thread ID
69// u4 method ID | method action
70// u4 time delta since start, in usec
71// u4 wall time since start, in usec (when clock == "dual" only)
72//
73// 32 bits of microseconds is 70 minutes.
74//
75// All values are stored in little-endian order.
76
Ian Rogers62d6c772013-02-27 08:32:07 -080077enum TraceAction {
78 kTraceMethodEnter = 0x00, // method entry
79 kTraceMethodExit = 0x01, // method exit
80 kTraceUnroll = 0x02, // method exited by exception unrolling
81 // 0x03 currently unused
82 kTraceMethodActionMask = 0x03, // two bits
83};
84
jeffhaoa9ef3fd2011-12-13 18:33:43 -080085static const char kTraceTokenChar = '*';
86static const uint16_t kTraceHeaderLength = 32;
87static const uint32_t kTraceMagicValue = 0x574f4c53;
88static const uint16_t kTraceVersionSingleClock = 2;
89static const uint16_t kTraceVersionDualClock = 3;
90static const uint16_t kTraceRecordSizeSingleClock = 10; // using v2
91static const uint16_t kTraceRecordSizeDualClock = 14; // using v3 with two timestamps
92
Ian Rogers62d6c772013-02-27 08:32:07 -080093#if defined(HAVE_POSIX_CLOCKS)
94ProfilerClockSource Trace::default_clock_source_ = kProfilerClockSourceDual;
95#else
96ProfilerClockSource Trace::default_clock_source_ = kProfilerClockSourceWall;
97#endif
Elliott Hughese119a362012-05-22 17:37:06 -070098
Ian Rogers62d6c772013-02-27 08:32:07 -080099Trace* Trace::the_trace_ = NULL;
100
101static mirror::AbstractMethod* DecodeTraceMethodId(uint32_t tmid) {
102 return reinterpret_cast<mirror::AbstractMethod*>(tmid & ~kTraceMethodActionMask);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800103}
Elliott Hughese119a362012-05-22 17:37:06 -0700104
Ian Rogers62d6c772013-02-27 08:32:07 -0800105static TraceAction DecodeTraceAction(uint32_t tmid) {
106 return static_cast<TraceAction>(tmid & kTraceMethodActionMask);
107}
108
109static uint32_t EncodeTraceMethodAndAction(const mirror::AbstractMethod* method,
110 TraceAction action) {
111 uint32_t tmid = reinterpret_cast<uint32_t>(method) | action;
112 DCHECK_EQ(method, DecodeTraceMethodId(tmid));
113 return tmid;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800114}
115
Elliott Hughese119a362012-05-22 17:37:06 -0700116void Trace::SetDefaultClockSource(ProfilerClockSource clock_source) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800117#if defined(HAVE_POSIX_CLOCKS)
118 default_clock_source_ = clock_source;
119#else
120 if (clock_source != kProfilerClockSourceWall) {
121 LOG(WARNING) << "Ignoring tracing request to use ";
122 }
123#endif
124}
125
126static uint16_t GetTraceVersion(ProfilerClockSource clock_source) {
127 return (clock_source == kProfilerClockSourceDual) ? kTraceVersionDualClock
128 : kTraceVersionSingleClock;
129}
130
131static uint16_t GetRecordSize(ProfilerClockSource clock_source) {
132 return (clock_source == kProfilerClockSourceDual) ? kTraceRecordSizeDualClock
133 : kTraceRecordSizeSingleClock;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800134}
135
Elliott Hughese119a362012-05-22 17:37:06 -0700136bool Trace::UseThreadCpuClock() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800137 return (clock_source_ == kProfilerClockSourceThreadCpu) ||
138 (clock_source_ == kProfilerClockSourceDual);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800139}
140
Elliott Hughese119a362012-05-22 17:37:06 -0700141bool Trace::UseWallClock() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800142 return (clock_source_ == kProfilerClockSourceWall) ||
143 (clock_source_ == kProfilerClockSourceDual);
Elliott Hughese119a362012-05-22 17:37:06 -0700144}
145
146static void MeasureClockOverhead(Trace* trace) {
147 if (trace->UseThreadCpuClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800148 ThreadCpuMicroTime();
149 }
Elliott Hughese119a362012-05-22 17:37:06 -0700150 if (trace->UseWallClock()) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800151 MicroTime();
152 }
153}
154
Elliott Hughese119a362012-05-22 17:37:06 -0700155static uint32_t GetClockOverhead(Trace* trace) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800156 uint64_t start = ThreadCpuMicroTime();
157
158 for (int i = 4000; i > 0; i--) {
Elliott Hughese119a362012-05-22 17:37:06 -0700159 MeasureClockOverhead(trace);
160 MeasureClockOverhead(trace);
161 MeasureClockOverhead(trace);
162 MeasureClockOverhead(trace);
163 MeasureClockOverhead(trace);
164 MeasureClockOverhead(trace);
165 MeasureClockOverhead(trace);
166 MeasureClockOverhead(trace);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800167 }
168
169 uint64_t elapsed = ThreadCpuMicroTime() - start;
170 return uint32_t (elapsed / 32);
171}
172
Elliott Hughesffb465f2012-03-01 18:46:05 -0800173// TODO: put this somewhere with the big-endian equivalent used by JDWP.
174static void Append2LE(uint8_t* buf, uint16_t val) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800175 *buf++ = (uint8_t) val;
176 *buf++ = (uint8_t) (val >> 8);
177}
178
Elliott Hughesffb465f2012-03-01 18:46:05 -0800179// TODO: put this somewhere with the big-endian equivalent used by JDWP.
180static void Append4LE(uint8_t* buf, uint32_t val) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800181 *buf++ = (uint8_t) val;
182 *buf++ = (uint8_t) (val >> 8);
183 *buf++ = (uint8_t) (val >> 16);
184 *buf++ = (uint8_t) (val >> 24);
185}
186
Elliott Hughesffb465f2012-03-01 18:46:05 -0800187// TODO: put this somewhere with the big-endian equivalent used by JDWP.
188static void Append8LE(uint8_t* buf, uint64_t val) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800189 *buf++ = (uint8_t) val;
190 *buf++ = (uint8_t) (val >> 8);
191 *buf++ = (uint8_t) (val >> 16);
192 *buf++ = (uint8_t) (val >> 24);
193 *buf++ = (uint8_t) (val >> 32);
194 *buf++ = (uint8_t) (val >> 40);
195 *buf++ = (uint8_t) (val >> 48);
196 *buf++ = (uint8_t) (val >> 56);
197}
198
Ian Rogers62d6c772013-02-27 08:32:07 -0800199void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags,
200 bool direct_to_ddms) {
201 Thread* self = Thread::Current();
202 {
203 MutexLock mu(self, *Locks::trace_lock_);
204 if (the_trace_ != NULL) {
205 LOG(ERROR) << "Trace already in progress, ignoring this request";
206 return;
207 }
jeffhaoe343b762011-12-05 16:36:44 -0800208 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800209 Runtime* runtime = Runtime::Current();
210 runtime->GetThreadList()->SuspendAll();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800211
jeffhao2692b572011-12-16 15:42:28 -0800212 // Open trace file if not going directly to ddms.
Ian Rogers62d6c772013-02-27 08:32:07 -0800213 UniquePtr<File> trace_file;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800214 if (!direct_to_ddms) {
215 if (trace_fd < 0) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800216 trace_file.reset(OS::OpenFile(trace_filename, true));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800217 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -0800218 trace_file.reset(new File(trace_fd, "tracefile"));
Elliott Hughes76160052012-12-12 16:31:20 -0800219 trace_file->DisableAutoClose();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800220 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800221 if (trace_file.get() == NULL) {
jeffhaob5e81852012-03-12 11:15:45 -0700222 PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
Ian Rogers62d6c772013-02-27 08:32:07 -0800223 runtime->GetThreadList()->ResumeAll();
224 ScopedObjectAccess soa(self);
225 ThrowRuntimeException("Unable to open trace file '%s'", trace_filename);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800226 return;
227 }
228 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800229
jeffhao2692b572011-12-16 15:42:28 -0800230 // Create Trace object.
Ian Rogers62d6c772013-02-27 08:32:07 -0800231 {
232 MutexLock mu(self, *Locks::trace_lock_);
233 if(the_trace_ != NULL) {
234 LOG(ERROR) << "Trace already in progress, ignoring this request";
235 } else {
236 the_trace_ = new Trace(trace_file.release(), buffer_size, flags);
jeffhao0791adc2012-04-04 11:14:32 -0700237
Ian Rogers62d6c772013-02-27 08:32:07 -0800238 // Enable count of allocs if specified in the flags.
239 if ((flags && kTraceCountAllocs) != 0) {
240 runtime->SetStatsEnabled(true);
241 }
242
243 runtime->GetInstrumentation()->AddListener(the_trace_,
244 instrumentation::Instrumentation::kMethodEntered |
245 instrumentation::Instrumentation::kMethodExited |
246 instrumentation::Instrumentation::kMethodUnwind);
247 }
jeffhao0791adc2012-04-04 11:14:32 -0700248 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800249 runtime->GetThreadList()->ResumeAll();
jeffhao2692b572011-12-16 15:42:28 -0800250}
251
252void Trace::Stop() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800253 Runtime* runtime = Runtime::Current();
254 runtime->GetThreadList()->SuspendAll();
255 Trace* the_trace = NULL;
256 {
257 MutexLock mu(Thread::Current(), *Locks::trace_lock_);
258 if (the_trace_ == NULL) {
259 LOG(ERROR) << "Trace stop requested, but no trace currently running";
260 } else {
261 the_trace = the_trace_;
262 the_trace_ = NULL;
263 }
jeffhao2692b572011-12-16 15:42:28 -0800264 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800265 if (the_trace != NULL) {
266 the_trace->FinishTracing();
267 runtime->GetInstrumentation()->RemoveListener(the_trace,
268 instrumentation::Instrumentation::kMethodEntered |
269 instrumentation::Instrumentation::kMethodExited |
270 instrumentation::Instrumentation::kMethodUnwind);
271 delete the_trace;
272 }
273 runtime->GetThreadList()->ResumeAll();
jeffhao2692b572011-12-16 15:42:28 -0800274}
275
jeffhaob5e81852012-03-12 11:15:45 -0700276void Trace::Shutdown() {
Ian Rogers62d6c772013-02-27 08:32:07 -0800277 if (IsMethodTracingActive()) {
278 Stop();
jeffhaob5e81852012-03-12 11:15:45 -0700279 }
jeffhaob5e81852012-03-12 11:15:45 -0700280}
281
Ian Rogers62d6c772013-02-27 08:32:07 -0800282bool Trace::IsMethodTracingActive() {
283 MutexLock mu(Thread::Current(), *Locks::trace_lock_);
284 return the_trace_ != NULL;
285}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800286
Ian Rogers62d6c772013-02-27 08:32:07 -0800287Trace::Trace(File* trace_file, int buffer_size, int flags)
288 : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
289 clock_source_(default_clock_source_), buffer_size_(buffer_size), start_time_(MicroTime()),
290 cur_offset_(0), overflow_(false) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800291 // Set up the beginning of the trace.
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 uint16_t trace_version = GetTraceVersion(clock_source_);
jeffhao2692b572011-12-16 15:42:28 -0800293 memset(buf_.get(), 0, kTraceHeaderLength);
294 Append4LE(buf_.get(), kTraceMagicValue);
Ian Rogers62d6c772013-02-27 08:32:07 -0800295 Append2LE(buf_.get() + 4, trace_version);
jeffhao2692b572011-12-16 15:42:28 -0800296 Append2LE(buf_.get() + 6, kTraceHeaderLength);
297 Append8LE(buf_.get() + 8, start_time_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800298 if (trace_version >= kTraceVersionDualClock) {
299 uint16_t record_size = GetRecordSize(clock_source_);
300 Append2LE(buf_.get() + 16, record_size);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800301 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800302
jeffhao2692b572011-12-16 15:42:28 -0800303 // Update current offset.
304 cur_offset_ = kTraceHeaderLength;
Ian Rogers62d6c772013-02-27 08:32:07 -0800305}
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800306
Ian Rogers62d6c772013-02-27 08:32:07 -0800307static void DumpBuf(uint8_t* buf, size_t buf_size, ProfilerClockSource clock_source)
308 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
309 uint8_t* ptr = buf + kTraceHeaderLength;
310 uint8_t* end = buf + buf_size;
311
312 while (ptr < end) {
313 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
314 mirror::AbstractMethod* method = DecodeTraceMethodId(tmid);
315 TraceAction action = DecodeTraceAction(tmid);
316 LOG(INFO) << PrettyMethod(method) << " " << static_cast<int>(action);
317 ptr += GetRecordSize(clock_source);
318 }
jeffhaoe343b762011-12-05 16:36:44 -0800319}
320
jeffhao2692b572011-12-16 15:42:28 -0800321void Trace::FinishTracing() {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800322 // Compute elapsed time.
323 uint64_t elapsed = MicroTime() - start_time_;
324
325 size_t final_offset = cur_offset_;
Elliott Hughese119a362012-05-22 17:37:06 -0700326 uint32_t clock_overhead = GetClockOverhead(this);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800327
jeffhao0791adc2012-04-04 11:14:32 -0700328 if ((flags_ & kTraceCountAllocs) != 0) {
329 Runtime::Current()->SetStatsEnabled(false);
330 }
331
Ian Rogers62d6c772013-02-27 08:32:07 -0800332 std::set<mirror::AbstractMethod*> visited_methods;
333 GetVisitedMethods(final_offset, &visited_methods);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800334
335 std::ostringstream os;
336
337 os << StringPrintf("%cversion\n", kTraceTokenChar);
Ian Rogers62d6c772013-02-27 08:32:07 -0800338 os << StringPrintf("%d\n", GetTraceVersion(clock_source_));
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800339 os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
340 if (UseThreadCpuClock()) {
341 if (UseWallClock()) {
342 os << StringPrintf("clock=dual\n");
343 } else {
344 os << StringPrintf("clock=thread-cpu\n");
345 }
346 } else {
347 os << StringPrintf("clock=wall\n");
348 }
349 os << StringPrintf("elapsed-time-usec=%llu\n", elapsed);
Ian Rogers62d6c772013-02-27 08:32:07 -0800350 size_t num_records = (final_offset - kTraceHeaderLength) / GetRecordSize(clock_source_);
351 os << StringPrintf("num-method-calls=%zd\n", num_records);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800352 os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead);
353 os << StringPrintf("vm=art\n");
jeffhao0791adc2012-04-04 11:14:32 -0700354 if ((flags_ & kTraceCountAllocs) != 0) {
355 os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
356 os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
357 os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
358 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800359 os << StringPrintf("%cthreads\n", kTraceTokenChar);
360 DumpThreadList(os);
361 os << StringPrintf("%cmethods\n", kTraceTokenChar);
Ian Rogers62d6c772013-02-27 08:32:07 -0800362 DumpMethodList(os, visited_methods);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800363 os << StringPrintf("%cend\n", kTraceTokenChar);
364
365 std::string header(os.str());
jeffhao2692b572011-12-16 15:42:28 -0800366 if (trace_file_.get() == NULL) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700367 iovec iov[2];
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800368 iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
369 iov[0].iov_len = header.length();
jeffhao2692b572011-12-16 15:42:28 -0800370 iov[1].iov_base = buf_.get();
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800371 iov[1].iov_len = final_offset;
372 Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 const bool kDumpTraceInfo = false;
374 if (kDumpTraceInfo) {
375 LOG(INFO) << "Trace sent:\n" << header;
376 DumpBuf(buf_.get(), final_offset, clock_source_);
377 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800378 } else {
379 if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
jeffhao2692b572011-12-16 15:42:28 -0800380 !trace_file_->WriteFully(buf_.get(), final_offset)) {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700381 std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
382 PLOG(ERROR) << detail;
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 ThrowRuntimeException("%s", detail.c_str());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800384 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800385 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800386}
387
Ian Rogers62d6c772013-02-27 08:32:07 -0800388void Trace::DexPcMoved(Thread* thread, mirror::Object* this_object,
389 const mirror::AbstractMethod* method, uint32_t new_dex_pc) {
390 // We're not recorded to listen to this kind of event, so complain.
391 LOG(ERROR) << "Unexpected dex PC event in tracing " << PrettyMethod(method) << " " << new_dex_pc;
392};
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800393
Ian Rogers62d6c772013-02-27 08:32:07 -0800394void Trace::MethodEntered(Thread* thread, mirror::Object* this_object,
395 const mirror::AbstractMethod* method, uint32_t dex_pc) {
396 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodEntered);
397}
398
399void Trace::MethodExited(Thread* thread, mirror::Object* this_object,
400 const mirror::AbstractMethod* method, uint32_t dex_pc,
401 const JValue& return_value) {
402 UNUSED(return_value);
403 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodExited);
404}
405
406void Trace::MethodUnwind(Thread* thread, const mirror::AbstractMethod* method, uint32_t dex_pc) {
407 LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodUnwind);
408}
409
410void Trace::ExceptionCaught(Thread* thread, const ThrowLocation& throw_location,
411 mirror::AbstractMethod* catch_method, uint32_t catch_dex_pc,
412 mirror::Throwable* exception_object)
413 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
414 LOG(ERROR) << "Unexpected exception caught event in tracing";
415}
416
417void Trace::LogMethodTraceEvent(Thread* thread, const mirror::AbstractMethod* method,
418 instrumentation::Instrumentation::InstrumentationEvent event) {
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800419 // Advance cur_offset_ atomically.
420 int32_t new_offset;
421 int32_t old_offset;
422 do {
423 old_offset = cur_offset_;
Ian Rogers62d6c772013-02-27 08:32:07 -0800424 new_offset = old_offset + GetRecordSize(clock_source_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800425 if (new_offset > buffer_size_) {
426 overflow_ = true;
427 return;
428 }
429 } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0);
430
Ian Rogers62d6c772013-02-27 08:32:07 -0800431 TraceAction action = kTraceMethodEnter;
432 switch (event) {
433 case instrumentation::Instrumentation::kMethodEntered:
434 action = kTraceMethodEnter;
435 break;
436 case instrumentation::Instrumentation::kMethodExited:
437 action = kTraceMethodExit;
438 break;
439 case instrumentation::Instrumentation::kMethodUnwind:
440 action = kTraceUnroll;
441 break;
442 default:
443 UNIMPLEMENTED(FATAL) << "Unexpected event: " << event;
444 }
445
446 uint32_t method_value = EncodeTraceMethodAndAction(method, action);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800447
448 // Write data
jeffhao2692b572011-12-16 15:42:28 -0800449 uint8_t* ptr = buf_.get() + old_offset;
Ian Rogers62d6c772013-02-27 08:32:07 -0800450 Append2LE(ptr, thread->GetTid());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800451 Append4LE(ptr + 2, method_value);
452 ptr += 6;
453
454 if (UseThreadCpuClock()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800455 // TODO: this isn't vaguely thread safe.
456 SafeMap<Thread*, uint64_t>::iterator it = thread_clock_base_map_.find(thread);
457 uint32_t thread_clock_diff = 0;
458 if (UNLIKELY(it == thread_clock_base_map_.end())) {
459 // First event, the diff is 0, record the base time in the map.
460 uint64_t time = ThreadCpuMicroTime();
461 thread_clock_base_map_.Put(thread, time);
462 } else {
463 uint64_t thread_clock_base = it->second;
464 thread_clock_diff = ThreadCpuMicroTime() - thread_clock_base;
465 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800466 Append4LE(ptr, thread_clock_diff);
467 ptr += 4;
468 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800469 if (UseWallClock()) {
470 uint32_t wall_clock_diff = MicroTime() - start_time_;
471 Append4LE(ptr, wall_clock_diff);
472 }
473}
474
Ian Rogers62d6c772013-02-27 08:32:07 -0800475void Trace::GetVisitedMethods(size_t buf_size,
476 std::set<mirror::AbstractMethod*>* visited_methods) {
jeffhao2692b572011-12-16 15:42:28 -0800477 uint8_t* ptr = buf_.get() + kTraceHeaderLength;
Ian Rogers62d6c772013-02-27 08:32:07 -0800478 uint8_t* end = buf_.get() + buf_size;
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800479
480 while (ptr < end) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800481 uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
482 mirror::AbstractMethod* method = DecodeTraceMethodId(tmid);
483 visited_methods->insert(method);
484 ptr += GetRecordSize(clock_source_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800485 }
486}
487
Ian Rogers62d6c772013-02-27 08:32:07 -0800488void Trace::DumpMethodList(std::ostream& os,
489 const std::set<mirror::AbstractMethod*>& visited_methods) {
490 typedef std::set<mirror::AbstractMethod*>::const_iterator It; // TODO: C++0x auto
491 MethodHelper mh;
492 for (It it = visited_methods.begin(); it != visited_methods.end(); ++it) {
493 mirror::AbstractMethod* method = *it;
494 mh.ChangeMethod(method);
Ian Rogers0399dde2012-06-06 17:09:28 -0700495 os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800496 PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(),
Ian Rogers0399dde2012-06-06 17:09:28 -0700497 mh.GetSignature().c_str(), mh.GetDeclaringClassSourceFile());
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800498 }
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800499}
500
501static void DumpThread(Thread* t, void* arg) {
Elliott Hughesffb465f2012-03-01 18:46:05 -0800502 std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
503 std::string name;
504 t->GetThreadName(name);
505 os << t->GetTid() << "\t" << name << "\n";
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800506}
507
508void Trace::DumpThreadList(std::ostream& os) {
Ian Rogers81d425b2012-09-27 16:03:43 -0700509 Thread* self = Thread::Current();
510 Locks::thread_list_lock_->AssertNotHeld(self);
511 MutexLock mu(self, *Locks::thread_list_lock_);
jeffhaoa9ef3fd2011-12-13 18:33:43 -0800512 Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
jeffhaoe343b762011-12-05 16:36:44 -0800513}
514
jeffhaoe343b762011-12-05 16:36:44 -0800515} // namespace art