1 // Copyright (C) 2019 The Android Open Source Project 2 // Copyright (C) 2019 Google Inc. 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 #pragma once 16 17 // Library to perform tracing. Talks to platform-specific 18 // tracing libraries. 19 20 namespace android { 21 namespace base { 22 23 #ifdef HOST_BUILD 24 void initializeTracing(); 25 void enableTracing(); 26 void disableTracing(); 27 // Some platform tracing libraries such as Perfetto can be enabled/disabled at 28 // runtime. Allow the user to query if they are disabled or not, and take 29 // further action based on it. The use case is to enable/disable tracing on the 30 // host alongside. 31 bool isTracingEnabled(); 32 33 class ScopedTrace { 34 public: 35 ScopedTrace(const char* name); 36 ~ScopedTrace(); 37 }; 38 39 class ScopedTraceDerived : public ScopedTrace { 40 public: 41 void* member = nullptr; 42 }; 43 #endif 44 45 bool isTracingEnabled(); 46 47 class ScopedTraceGuest { 48 public: ScopedTraceGuest(const char * name)49 ScopedTraceGuest(const char* name) : name_(name) { 50 beginTraceImpl(name_); 51 } 52 ~ScopedTraceGuest()53 ~ScopedTraceGuest() { 54 endTraceImpl(name_); 55 } 56 private: 57 void beginTraceImpl(const char* name); 58 void endTraceImpl(const char* name); 59 60 const char* const name_; 61 }; 62 63 } // namespace base 64 } // namespace android 65 66 #define __AEMU_GENSYM2(x,y) x##y 67 #define __AEMU_GENSYM1(x,y) __AEMU_GENSYM2(x,y) 68 #define AEMU_GENSYM(x) __AEMU_GENSYM1(x,__COUNTER__) 69 70 #ifdef HOST_BUILD 71 #define AEMU_SCOPED_TRACE(tag) __attribute__ ((unused)) android::base::ScopedTrace AEMU_GENSYM(aemuScopedTrace_)(tag) 72 #else 73 #define AEMU_SCOPED_TRACE(tag) __attribute__ ((unused)) android::base::ScopedTraceGuest AEMU_GENSYM(aemuScopedTrace_)(tag) 74 #endif 75