xref: /aosp_15_r20/frameworks/native/libs/binder/include/binder/Trace.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2022 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 #pragma once
18 
19 #include <stdint.h>
20 
21 #if __has_include(<cutils/trace.h>)
22 #include <cutils/trace.h>
23 #endif
24 
25 #include <binder/Common.h>
26 
27 #ifdef ATRACE_TAG_AIDL
28 #if ATRACE_TAG_AIDL != (1 << 24)
29 #error "Mismatched ATRACE_TAG_AIDL definitions"
30 #endif
31 #else
32 #define ATRACE_TAG_AIDL (1 << 24)
33 #endif
34 
35 namespace android {
36 namespace binder {
37 
38 // Forward declarations from internal OS.h
39 namespace os {
40 // Trampoline functions allowing generated aidls to trace binder transactions without depending on
41 // libcutils/libutils
42 void trace_begin(uint64_t tag, const char* name);
43 void trace_end(uint64_t tag);
44 void trace_int(uint64_t tag, const char* name, int32_t value);
45 uint64_t get_trace_enabled_tags();
46 } // namespace os
47 
48 class LIBBINDER_EXPORTED ScopedTrace {
49 public:
ScopedTrace(uint64_t tag,const char * name)50     inline ScopedTrace(uint64_t tag, const char* name) : mTag(tag) { os::trace_begin(mTag, name); }
51 
~ScopedTrace()52     inline ~ScopedTrace() { os::trace_end(mTag); }
53 
54 private:
55     uint64_t mTag;
56 };
57 
58 } // namespace binder
59 } // namespace android
60