trace.cc revision bf9fc581e8870faddbd320a935f9a627da724c48
1/*
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 */
16
17#include "trace.h"
18
19#include <sys/uio.h>
20
21#define ATRACE_TAG ATRACE_TAG_DALVIK
22#include "cutils/trace.h"
23
24#include "base/stl_util.h"
25#include "base/unix_file/fd_file.h"
26#include "class_linker.h"
27#include "common_throws.h"
28#include "debugger.h"
29#include "dex_file-inl.h"
30#include "instrumentation.h"
31#include "mirror/art_method-inl.h"
32#include "mirror/class-inl.h"
33#include "mirror/dex_cache.h"
34#include "mirror/object_array-inl.h"
35#include "mirror/object-inl.h"
36#include "os.h"
37#include "scoped_thread_state_change.h"
38#include "ScopedLocalRef.h"
39#include "thread.h"
40#include "thread_list.h"
41#include "entrypoints/quick/quick_entrypoints.h"
42
43namespace art {
44
45// File format:
46//     header
47//     record 0
48//     record 1
49//     ...
50//
51// Header format:
52//     u4  magic ('SLOW')
53//     u2  version
54//     u2  offset to data
55//     u8  start date/time in usec
56//     u2  record size in bytes (version >= 2 only)
57//     ... padding to 32 bytes
58//
59// Record format v1:
60//     u1  thread ID
61//     u4  method ID | method action
62//     u4  time delta since start, in usec
63//
64// Record format v2:
65//     u2  thread ID
66//     u4  method ID | method action
67//     u4  time delta since start, in usec
68//
69// Record format v3:
70//     u2  thread ID
71//     u4  method ID | method action
72//     u4  time delta since start, in usec
73//     u4  wall time since start, in usec (when clock == "dual" only)
74//
75// 32 bits of microseconds is 70 minutes.
76//
77// All values are stored in little-endian order.
78
79enum TraceAction {
80    kTraceMethodEnter = 0x00,       // method entry
81    kTraceMethodExit = 0x01,        // method exit
82    kTraceUnroll = 0x02,            // method exited by exception unrolling
83    // 0x03 currently unused
84    kTraceMethodActionMask = 0x03,  // two bits
85};
86
87class BuildStackTraceVisitor : public StackVisitor {
88 public:
89  explicit BuildStackTraceVisitor(Thread* thread) : StackVisitor(thread, NULL),
90      method_trace_(Trace::AllocStackTrace()) {}
91
92  bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
93    mirror::ArtMethod* m = GetMethod();
94    // Ignore runtime frames (in particular callee save).
95    if (!m->IsRuntimeMethod()) {
96      method_trace_->push_back(m);
97    }
98    return true;
99  }
100
101  // Returns a stack trace where the topmost frame corresponds with the first element of the vector.
102  std::vector<mirror::ArtMethod*>* GetStackTrace() const {
103    return method_trace_;
104  }
105
106 private:
107  std::vector<mirror::ArtMethod*>* const method_trace_;
108};
109
110static const char     kTraceTokenChar             = '*';
111static const uint16_t kTraceHeaderLength          = 32;
112static const uint32_t kTraceMagicValue            = 0x574f4c53;
113static const uint16_t kTraceVersionSingleClock    = 2;
114static const uint16_t kTraceVersionDualClock      = 3;
115static const uint16_t kTraceRecordSizeSingleClock = 10;  // using v2
116static const uint16_t kTraceRecordSizeDualClock   = 14;  // using v3 with two timestamps
117
118TraceClockSource Trace::default_clock_source_ = kDefaultTraceClockSource;
119
120Trace* volatile Trace::the_trace_ = NULL;
121pthread_t Trace::sampling_pthread_ = 0U;
122std::unique_ptr<std::vector<mirror::ArtMethod*>> Trace::temp_stack_trace_;
123
124static mirror::ArtMethod* DecodeTraceMethodId(uint32_t tmid) {
125  return reinterpret_cast<mirror::ArtMethod*>(tmid & ~kTraceMethodActionMask);
126}
127
128static TraceAction DecodeTraceAction(uint32_t tmid) {
129  return static_cast<TraceAction>(tmid & kTraceMethodActionMask);
130}
131
132static uint32_t EncodeTraceMethodAndAction(mirror::ArtMethod* method,
133                                           TraceAction action) {
134  uint32_t tmid = PointerToLowMemUInt32(method) | action;
135  DCHECK_EQ(method, DecodeTraceMethodId(tmid));
136  return tmid;
137}
138
139std::vector<mirror::ArtMethod*>* Trace::AllocStackTrace() {
140  if (temp_stack_trace_.get() != NULL) {
141    return temp_stack_trace_.release();
142  } else {
143    return new std::vector<mirror::ArtMethod*>();
144  }
145}
146
147void Trace::FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace) {
148  stack_trace->clear();
149  temp_stack_trace_.reset(stack_trace);
150}
151
152void Trace::SetDefaultClockSource(TraceClockSource clock_source) {
153#if defined(__linux__)
154  default_clock_source_ = clock_source;
155#else
156  if (clock_source != TraceClockSource::kWall) {
157    LOG(WARNING) << "Ignoring tracing request to use CPU time.";
158  }
159#endif
160}
161
162static uint16_t GetTraceVersion(TraceClockSource clock_source) {
163  return (clock_source == TraceClockSource::kDual) ? kTraceVersionDualClock
164                                                    : kTraceVersionSingleClock;
165}
166
167static uint16_t GetRecordSize(TraceClockSource clock_source) {
168  return (clock_source == TraceClockSource::kDual) ? kTraceRecordSizeDualClock
169                                                    : kTraceRecordSizeSingleClock;
170}
171
172bool Trace::UseThreadCpuClock() {
173  return (clock_source_ == TraceClockSource::kThreadCpu) ||
174      (clock_source_ == TraceClockSource::kDual);
175}
176
177bool Trace::UseWallClock() {
178  return (clock_source_ == TraceClockSource::kWall) ||
179      (clock_source_ == TraceClockSource::kDual);
180}
181
182void Trace::MeasureClockOverhead() {
183  if (UseThreadCpuClock()) {
184    Thread::Current()->GetCpuMicroTime();
185  }
186  if (UseWallClock()) {
187    MicroTime();
188  }
189}
190
191// Compute an average time taken to measure clocks.
192uint32_t Trace::GetClockOverheadNanoSeconds() {
193  Thread* self = Thread::Current();
194  uint64_t start = self->GetCpuMicroTime();
195
196  for (int i = 4000; i > 0; i--) {
197    MeasureClockOverhead();
198    MeasureClockOverhead();
199    MeasureClockOverhead();
200    MeasureClockOverhead();
201    MeasureClockOverhead();
202    MeasureClockOverhead();
203    MeasureClockOverhead();
204    MeasureClockOverhead();
205  }
206
207  uint64_t elapsed_us = self->GetCpuMicroTime() - start;
208  return static_cast<uint32_t>(elapsed_us / 32);
209}
210
211// TODO: put this somewhere with the big-endian equivalent used by JDWP.
212static void Append2LE(uint8_t* buf, uint16_t val) {
213  *buf++ = static_cast<uint8_t>(val);
214  *buf++ = static_cast<uint8_t>(val >> 8);
215}
216
217// TODO: put this somewhere with the big-endian equivalent used by JDWP.
218static void Append4LE(uint8_t* buf, uint32_t val) {
219  *buf++ = static_cast<uint8_t>(val);
220  *buf++ = static_cast<uint8_t>(val >> 8);
221  *buf++ = static_cast<uint8_t>(val >> 16);
222  *buf++ = static_cast<uint8_t>(val >> 24);
223}
224
225// TODO: put this somewhere with the big-endian equivalent used by JDWP.
226static void Append8LE(uint8_t* buf, uint64_t val) {
227  *buf++ = static_cast<uint8_t>(val);
228  *buf++ = static_cast<uint8_t>(val >> 8);
229  *buf++ = static_cast<uint8_t>(val >> 16);
230  *buf++ = static_cast<uint8_t>(val >> 24);
231  *buf++ = static_cast<uint8_t>(val >> 32);
232  *buf++ = static_cast<uint8_t>(val >> 40);
233  *buf++ = static_cast<uint8_t>(val >> 48);
234  *buf++ = static_cast<uint8_t>(val >> 56);
235}
236
237static void GetSample(Thread* thread, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
238  BuildStackTraceVisitor build_trace_visitor(thread);
239  build_trace_visitor.WalkStack();
240  std::vector<mirror::ArtMethod*>* stack_trace = build_trace_visitor.GetStackTrace();
241  Trace* the_trace = reinterpret_cast<Trace*>(arg);
242  the_trace->CompareAndUpdateStackTrace(thread, stack_trace);
243}
244
245static void ClearThreadStackTraceAndClockBase(Thread* thread ATTRIBUTE_UNUSED,
246                                              void* arg ATTRIBUTE_UNUSED) {
247  thread->SetTraceClockBase(0);
248  std::vector<mirror::ArtMethod*>* stack_trace = thread->GetStackTraceSample();
249  thread->SetStackTraceSample(NULL);
250  delete stack_trace;
251}
252
253void Trace::CompareAndUpdateStackTrace(Thread* thread,
254                                       std::vector<mirror::ArtMethod*>* stack_trace) {
255  CHECK_EQ(pthread_self(), sampling_pthread_);
256  std::vector<mirror::ArtMethod*>* old_stack_trace = thread->GetStackTraceSample();
257  // Update the thread's stack trace sample.
258  thread->SetStackTraceSample(stack_trace);
259  // Read timer clocks to use for all events in this trace.
260  uint32_t thread_clock_diff = 0;
261  uint32_t wall_clock_diff = 0;
262  ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
263  if (old_stack_trace == NULL) {
264    // If there's no previous stack trace sample for this thread, log an entry event for all
265    // methods in the trace.
266    for (std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin();
267         rit != stack_trace->rend(); ++rit) {
268      LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered,
269                          thread_clock_diff, wall_clock_diff);
270    }
271  } else {
272    // If there's a previous stack trace for this thread, diff the traces and emit entry and exit
273    // events accordingly.
274    std::vector<mirror::ArtMethod*>::reverse_iterator old_rit = old_stack_trace->rbegin();
275    std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin();
276    // Iterate bottom-up over both traces until there's a difference between them.
277    while (old_rit != old_stack_trace->rend() && rit != stack_trace->rend() && *old_rit == *rit) {
278      old_rit++;
279      rit++;
280    }
281    // Iterate top-down over the old trace until the point where they differ, emitting exit events.
282    for (std::vector<mirror::ArtMethod*>::iterator old_it = old_stack_trace->begin();
283         old_it != old_rit.base(); ++old_it) {
284      LogMethodTraceEvent(thread, *old_it, instrumentation::Instrumentation::kMethodExited,
285                          thread_clock_diff, wall_clock_diff);
286    }
287    // Iterate bottom-up over the new trace from the point where they differ, emitting entry events.
288    for (; rit != stack_trace->rend(); ++rit) {
289      LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered,
290                          thread_clock_diff, wall_clock_diff);
291    }
292    FreeStackTrace(old_stack_trace);
293  }
294}
295
296void* Trace::RunSamplingThread(void* arg) {
297  Runtime* runtime = Runtime::Current();
298  intptr_t interval_us = reinterpret_cast<intptr_t>(arg);
299  CHECK_GE(interval_us, 0);
300  CHECK(runtime->AttachCurrentThread("Sampling Profiler", true, runtime->GetSystemThreadGroup(),
301                                     !runtime->IsAotCompiler()));
302
303  while (true) {
304    usleep(interval_us);
305    ATRACE_BEGIN("Profile sampling");
306    Thread* self = Thread::Current();
307    Trace* the_trace;
308    {
309      MutexLock mu(self, *Locks::trace_lock_);
310      the_trace = the_trace_;
311      if (the_trace == NULL) {
312        break;
313      }
314    }
315
316    runtime->GetThreadList()->SuspendAll(__FUNCTION__);
317    {
318      MutexLock mu(self, *Locks::thread_list_lock_);
319      runtime->GetThreadList()->ForEach(GetSample, the_trace);
320    }
321    runtime->GetThreadList()->ResumeAll();
322    ATRACE_END();
323  }
324
325  runtime->DetachCurrentThread();
326  return NULL;
327}
328
329void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags,
330                  bool direct_to_ddms, bool sampling_enabled, int interval_us) {
331  Thread* self = Thread::Current();
332  {
333    MutexLock mu(self, *Locks::trace_lock_);
334    if (the_trace_ != NULL) {
335      LOG(ERROR) << "Trace already in progress, ignoring this request";
336      return;
337    }
338  }
339
340  // Check interval if sampling is enabled
341  if (sampling_enabled && interval_us <= 0) {
342    LOG(ERROR) << "Invalid sampling interval: " << interval_us;
343    ScopedObjectAccess soa(self);
344    ThrowRuntimeException("Invalid sampling interval: %d", interval_us);
345    return;
346  }
347
348  // Open trace file if not going directly to ddms.
349  std::unique_ptr<File> trace_file;
350  if (!direct_to_ddms) {
351    if (trace_fd < 0) {
352      trace_file.reset(OS::CreateEmptyFile(trace_filename));
353    } else {
354      trace_file.reset(new File(trace_fd, "tracefile"));
355      trace_file->DisableAutoClose();
356    }
357    if (trace_file.get() == NULL) {
358      PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'";
359      ScopedObjectAccess soa(self);
360      ThrowRuntimeException("Unable to open trace file '%s'", trace_filename);
361      return;
362    }
363  }
364
365  Runtime* runtime = Runtime::Current();
366
367  // Enable count of allocs if specified in the flags.
368  bool enable_stats = false;
369
370  runtime->GetThreadList()->SuspendAll(__FUNCTION__);
371
372  // Create Trace object.
373  {
374    MutexLock mu(self, *Locks::trace_lock_);
375    if (the_trace_ != NULL) {
376      LOG(ERROR) << "Trace already in progress, ignoring this request";
377    } else {
378      enable_stats = (flags && kTraceCountAllocs) != 0;
379      the_trace_ = new Trace(trace_file.release(), buffer_size, flags, sampling_enabled);
380      if (sampling_enabled) {
381        CHECK_PTHREAD_CALL(pthread_create, (&sampling_pthread_, NULL, &RunSamplingThread,
382                                            reinterpret_cast<void*>(interval_us)),
383                                            "Sampling profiler thread");
384      } else {
385        runtime->GetInstrumentation()->AddListener(the_trace_,
386                                                   instrumentation::Instrumentation::kMethodEntered |
387                                                   instrumentation::Instrumentation::kMethodExited |
388                                                   instrumentation::Instrumentation::kMethodUnwind);
389        runtime->GetInstrumentation()->EnableMethodTracing();
390      }
391    }
392  }
393
394  runtime->GetThreadList()->ResumeAll();
395
396  // Can't call this when holding the mutator lock.
397  if (enable_stats) {
398    runtime->SetStatsEnabled(true);
399  }
400}
401
402void Trace::Stop() {
403  bool stop_alloc_counting = false;
404  Runtime* const runtime = Runtime::Current();
405  Trace* the_trace = nullptr;
406  pthread_t sampling_pthread = 0U;
407  {
408    MutexLock mu(Thread::Current(), *Locks::trace_lock_);
409    if (the_trace_ == NULL) {
410      LOG(ERROR) << "Trace stop requested, but no trace currently running";
411    } else {
412      the_trace = the_trace_;
413      the_trace_ = NULL;
414      sampling_pthread = sampling_pthread_;
415    }
416  }
417  // Make sure that we join before we delete the trace since we don't want to have
418  // the sampling thread access a stale pointer. This finishes since the sampling thread exits when
419  // the_trace_ is null.
420  if (sampling_pthread != 0U) {
421    CHECK_PTHREAD_CALL(pthread_join, (sampling_pthread, NULL), "sampling thread shutdown");
422    sampling_pthread_ = 0U;
423  }
424  runtime->GetThreadList()->SuspendAll(__FUNCTION__);
425  if (the_trace != nullptr) {
426    stop_alloc_counting = (the_trace->flags_ & kTraceCountAllocs) != 0;
427    the_trace->FinishTracing();
428
429    if (the_trace->sampling_enabled_) {
430      MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
431      runtime->GetThreadList()->ForEach(ClearThreadStackTraceAndClockBase, nullptr);
432    } else {
433      runtime->GetInstrumentation()->DisableMethodTracing();
434      runtime->GetInstrumentation()->RemoveListener(
435          the_trace, instrumentation::Instrumentation::kMethodEntered |
436          instrumentation::Instrumentation::kMethodExited |
437          instrumentation::Instrumentation::kMethodUnwind);
438    }
439    if (the_trace->trace_file_.get() != nullptr) {
440      // Do not try to erase, so flush and close explicitly.
441      if (the_trace->trace_file_->Flush() != 0) {
442        PLOG(ERROR) << "Could not flush trace file.";
443      }
444      if (the_trace->trace_file_->Close() != 0) {
445        PLOG(ERROR) << "Could not close trace file.";
446      }
447    }
448    delete the_trace;
449  }
450  runtime->GetThreadList()->ResumeAll();
451  if (stop_alloc_counting) {
452    // Can be racy since SetStatsEnabled is not guarded by any locks.
453    runtime->SetStatsEnabled(false);
454  }
455}
456
457void Trace::Shutdown() {
458  if (GetMethodTracingMode() != kTracingInactive) {
459    Stop();
460  }
461}
462
463TracingMode Trace::GetMethodTracingMode() {
464  MutexLock mu(Thread::Current(), *Locks::trace_lock_);
465  if (the_trace_ == NULL) {
466    return kTracingInactive;
467  } else if (the_trace_->sampling_enabled_) {
468    return kSampleProfilingActive;
469  } else {
470    return kMethodTracingActive;
471  }
472}
473
474Trace::Trace(File* trace_file, int buffer_size, int flags, bool sampling_enabled)
475    : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags),
476      sampling_enabled_(sampling_enabled), clock_source_(default_clock_source_),
477      buffer_size_(buffer_size), start_time_(MicroTime()),
478      clock_overhead_ns_(GetClockOverheadNanoSeconds()), cur_offset_(0), overflow_(false) {
479  // Set up the beginning of the trace.
480  uint16_t trace_version = GetTraceVersion(clock_source_);
481  memset(buf_.get(), 0, kTraceHeaderLength);
482  Append4LE(buf_.get(), kTraceMagicValue);
483  Append2LE(buf_.get() + 4, trace_version);
484  Append2LE(buf_.get() + 6, kTraceHeaderLength);
485  Append8LE(buf_.get() + 8, start_time_);
486  if (trace_version >= kTraceVersionDualClock) {
487    uint16_t record_size = GetRecordSize(clock_source_);
488    Append2LE(buf_.get() + 16, record_size);
489  }
490
491  // Update current offset.
492  cur_offset_.StoreRelaxed(kTraceHeaderLength);
493}
494
495static void DumpBuf(uint8_t* buf, size_t buf_size, TraceClockSource clock_source)
496    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
497  uint8_t* ptr = buf + kTraceHeaderLength;
498  uint8_t* end = buf + buf_size;
499
500  while (ptr < end) {
501    uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
502    mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
503    TraceAction action = DecodeTraceAction(tmid);
504    LOG(INFO) << PrettyMethod(method) << " " << static_cast<int>(action);
505    ptr += GetRecordSize(clock_source);
506  }
507}
508
509void Trace::FinishTracing() {
510  // Compute elapsed time.
511  uint64_t elapsed = MicroTime() - start_time_;
512
513  size_t final_offset = cur_offset_.LoadRelaxed();
514
515  std::set<mirror::ArtMethod*> visited_methods;
516  GetVisitedMethods(final_offset, &visited_methods);
517
518  std::ostringstream os;
519
520  os << StringPrintf("%cversion\n", kTraceTokenChar);
521  os << StringPrintf("%d\n", GetTraceVersion(clock_source_));
522  os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false");
523  if (UseThreadCpuClock()) {
524    if (UseWallClock()) {
525      os << StringPrintf("clock=dual\n");
526    } else {
527      os << StringPrintf("clock=thread-cpu\n");
528    }
529  } else {
530    os << StringPrintf("clock=wall\n");
531  }
532  os << StringPrintf("elapsed-time-usec=%" PRIu64 "\n", elapsed);
533  size_t num_records = (final_offset - kTraceHeaderLength) / GetRecordSize(clock_source_);
534  os << StringPrintf("num-method-calls=%zd\n", num_records);
535  os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead_ns_);
536  os << StringPrintf("vm=art\n");
537  if ((flags_ & kTraceCountAllocs) != 0) {
538    os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS));
539    os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES));
540    os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS));
541  }
542  os << StringPrintf("%cthreads\n", kTraceTokenChar);
543  DumpThreadList(os);
544  os << StringPrintf("%cmethods\n", kTraceTokenChar);
545  DumpMethodList(os, visited_methods);
546  os << StringPrintf("%cend\n", kTraceTokenChar);
547
548  std::string header(os.str());
549  if (trace_file_.get() == NULL) {
550    iovec iov[2];
551    iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str()));
552    iov[0].iov_len = header.length();
553    iov[1].iov_base = buf_.get();
554    iov[1].iov_len = final_offset;
555    Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2);
556    const bool kDumpTraceInfo = false;
557    if (kDumpTraceInfo) {
558      LOG(INFO) << "Trace sent:\n" << header;
559      DumpBuf(buf_.get(), final_offset, clock_source_);
560    }
561  } else {
562    if (!trace_file_->WriteFully(header.c_str(), header.length()) ||
563        !trace_file_->WriteFully(buf_.get(), final_offset)) {
564      std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno)));
565      PLOG(ERROR) << detail;
566      ThrowRuntimeException("%s", detail.c_str());
567    }
568  }
569}
570
571void Trace::DexPcMoved(Thread* thread, mirror::Object* this_object,
572                       mirror::ArtMethod* method, uint32_t new_dex_pc) {
573  UNUSED(thread, this_object, method, new_dex_pc);
574  // We're not recorded to listen to this kind of event, so complain.
575  LOG(ERROR) << "Unexpected dex PC event in tracing " << PrettyMethod(method) << " " << new_dex_pc;
576}
577
578void Trace::FieldRead(Thread* thread, mirror::Object* this_object,
579                       mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field)
580    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
581  UNUSED(thread, this_object, method, dex_pc, field);
582  // We're not recorded to listen to this kind of event, so complain.
583  LOG(ERROR) << "Unexpected field read event in tracing " << PrettyMethod(method) << " " << dex_pc;
584}
585
586void Trace::FieldWritten(Thread* thread, mirror::Object* this_object,
587                          mirror::ArtMethod* method, uint32_t dex_pc, mirror::ArtField* field,
588                          const JValue& field_value)
589    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
590  UNUSED(thread, this_object, method, dex_pc, field, field_value);
591  // We're not recorded to listen to this kind of event, so complain.
592  LOG(ERROR) << "Unexpected field write event in tracing " << PrettyMethod(method) << " " << dex_pc;
593}
594
595void Trace::MethodEntered(Thread* thread, mirror::Object* this_object ATTRIBUTE_UNUSED,
596                          mirror::ArtMethod* method, uint32_t dex_pc ATTRIBUTE_UNUSED) {
597  uint32_t thread_clock_diff = 0;
598  uint32_t wall_clock_diff = 0;
599  ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
600  LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodEntered,
601                      thread_clock_diff, wall_clock_diff);
602}
603
604void Trace::MethodExited(Thread* thread, mirror::Object* this_object ATTRIBUTE_UNUSED,
605                         mirror::ArtMethod* method, uint32_t dex_pc ATTRIBUTE_UNUSED,
606                         const JValue& return_value ATTRIBUTE_UNUSED) {
607  uint32_t thread_clock_diff = 0;
608  uint32_t wall_clock_diff = 0;
609  ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
610  LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodExited,
611                      thread_clock_diff, wall_clock_diff);
612}
613
614void Trace::MethodUnwind(Thread* thread, mirror::Object* this_object ATTRIBUTE_UNUSED,
615                         mirror::ArtMethod* method, uint32_t dex_pc ATTRIBUTE_UNUSED) {
616  uint32_t thread_clock_diff = 0;
617  uint32_t wall_clock_diff = 0;
618  ReadClocks(thread, &thread_clock_diff, &wall_clock_diff);
619  LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodUnwind,
620                      thread_clock_diff, wall_clock_diff);
621}
622
623void Trace::ExceptionCaught(Thread* thread, mirror::Throwable* exception_object)
624    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
625  UNUSED(thread, exception_object);
626  LOG(ERROR) << "Unexpected exception caught event in tracing";
627}
628
629void Trace::BackwardBranch(Thread* /*thread*/, mirror::ArtMethod* method,
630                           int32_t /*dex_pc_offset*/)
631      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
632  LOG(ERROR) << "Unexpected backward branch event in tracing" << PrettyMethod(method);
633}
634
635void Trace::ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff) {
636  if (UseThreadCpuClock()) {
637    uint64_t clock_base = thread->GetTraceClockBase();
638    if (UNLIKELY(clock_base == 0)) {
639      // First event, record the base time in the map.
640      uint64_t time = thread->GetCpuMicroTime();
641      thread->SetTraceClockBase(time);
642    } else {
643      *thread_clock_diff = thread->GetCpuMicroTime() - clock_base;
644    }
645  }
646  if (UseWallClock()) {
647    *wall_clock_diff = MicroTime() - start_time_;
648  }
649}
650
651void Trace::LogMethodTraceEvent(Thread* thread, mirror::ArtMethod* method,
652                                instrumentation::Instrumentation::InstrumentationEvent event,
653                                uint32_t thread_clock_diff, uint32_t wall_clock_diff) {
654  // Advance cur_offset_ atomically.
655  int32_t new_offset;
656  int32_t old_offset;
657  do {
658    old_offset = cur_offset_.LoadRelaxed();
659    new_offset = old_offset + GetRecordSize(clock_source_);
660    if (new_offset > buffer_size_) {
661      overflow_ = true;
662      return;
663    }
664  } while (!cur_offset_.CompareExchangeWeakSequentiallyConsistent(old_offset, new_offset));
665
666  TraceAction action = kTraceMethodEnter;
667  switch (event) {
668    case instrumentation::Instrumentation::kMethodEntered:
669      action = kTraceMethodEnter;
670      break;
671    case instrumentation::Instrumentation::kMethodExited:
672      action = kTraceMethodExit;
673      break;
674    case instrumentation::Instrumentation::kMethodUnwind:
675      action = kTraceUnroll;
676      break;
677    default:
678      UNIMPLEMENTED(FATAL) << "Unexpected event: " << event;
679  }
680
681  uint32_t method_value = EncodeTraceMethodAndAction(method, action);
682
683  // Write data
684  uint8_t* ptr = buf_.get() + old_offset;
685  Append2LE(ptr, thread->GetTid());
686  Append4LE(ptr + 2, method_value);
687  ptr += 6;
688
689  if (UseThreadCpuClock()) {
690    Append4LE(ptr, thread_clock_diff);
691    ptr += 4;
692  }
693  if (UseWallClock()) {
694    Append4LE(ptr, wall_clock_diff);
695  }
696}
697
698void Trace::GetVisitedMethods(size_t buf_size,
699                              std::set<mirror::ArtMethod*>* visited_methods) {
700  uint8_t* ptr = buf_.get() + kTraceHeaderLength;
701  uint8_t* end = buf_.get() + buf_size;
702
703  while (ptr < end) {
704    uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24);
705    mirror::ArtMethod* method = DecodeTraceMethodId(tmid);
706    visited_methods->insert(method);
707    ptr += GetRecordSize(clock_source_);
708  }
709}
710
711void Trace::DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods) {
712  for (const auto& method : visited_methods) {
713    os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method,
714        PrettyDescriptor(method->GetDeclaringClassDescriptor()).c_str(), method->GetName(),
715        method->GetSignature().ToString().c_str(), method->GetDeclaringClassSourceFile());
716  }
717}
718
719static void DumpThread(Thread* t, void* arg) {
720  std::ostream& os = *reinterpret_cast<std::ostream*>(arg);
721  std::string name;
722  t->GetThreadName(name);
723  os << t->GetTid() << "\t" << name << "\n";
724}
725
726void Trace::DumpThreadList(std::ostream& os) {
727  Thread* self = Thread::Current();
728  for (auto it : exited_threads_) {
729    os << it.first << "\t" << it.second << "\n";
730  }
731  Locks::thread_list_lock_->AssertNotHeld(self);
732  MutexLock mu(self, *Locks::thread_list_lock_);
733  Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os);
734}
735
736void Trace::StoreExitingThreadInfo(Thread* thread) {
737  MutexLock mu(thread, *Locks::trace_lock_);
738  if (the_trace_ != nullptr) {
739    std::string name;
740    thread->GetThreadName(name);
741    // The same thread/tid may be used multiple times. As SafeMap::Put does not allow to override
742    // a previous mapping, use SafeMap::Overwrite.
743    the_trace_->exited_threads_.Overwrite(thread->GetTid(), name);
744  }
745}
746
747}  // namespace art
748