xref: /aosp_15_r20/external/oboe/src/common/Trace.cpp (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1*05767d91SRobert Wu /*
2*05767d91SRobert Wu  * Copyright 2018 The Android Open Source Project
3*05767d91SRobert Wu  *
4*05767d91SRobert Wu  * Licensed under the Apache License, Version 2.0 (the "License");
5*05767d91SRobert Wu  * you may not use this file except in compliance with the License.
6*05767d91SRobert Wu  * You may obtain a copy of the License at
7*05767d91SRobert Wu  *
8*05767d91SRobert Wu  *      http://www.apache.org/licenses/LICENSE-2.0
9*05767d91SRobert Wu  *
10*05767d91SRobert Wu  * Unless required by applicable law or agreed to in writing, software
11*05767d91SRobert Wu  * distributed under the License is distributed on an "AS IS" BASIS,
12*05767d91SRobert Wu  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*05767d91SRobert Wu  * See the License for the specific language governing permissions and
14*05767d91SRobert Wu  * limitations under the License.
15*05767d91SRobert Wu  */
16*05767d91SRobert Wu 
17*05767d91SRobert Wu #include <dlfcn.h>
18*05767d91SRobert Wu #include <cstdio>
19*05767d91SRobert Wu #include "Trace.h"
20*05767d91SRobert Wu #include "OboeDebug.h"
21*05767d91SRobert Wu 
22*05767d91SRobert Wu static char buffer[256];
23*05767d91SRobert Wu 
24*05767d91SRobert Wu // Tracing functions
25*05767d91SRobert Wu static void *(*ATrace_beginSection)(const char *sectionName);
26*05767d91SRobert Wu 
27*05767d91SRobert Wu static void *(*ATrace_endSection)();
28*05767d91SRobert Wu 
29*05767d91SRobert Wu typedef void *(*fp_ATrace_beginSection)(const char *sectionName);
30*05767d91SRobert Wu 
31*05767d91SRobert Wu typedef void *(*fp_ATrace_endSection)();
32*05767d91SRobert Wu 
33*05767d91SRobert Wu bool Trace::mIsTracingSupported = false;
34*05767d91SRobert Wu 
beginSection(const char * format,...)35*05767d91SRobert Wu void Trace::beginSection(const char *format, ...){
36*05767d91SRobert Wu 
37*05767d91SRobert Wu     if (mIsTracingSupported) {
38*05767d91SRobert Wu         va_list va;
39*05767d91SRobert Wu         va_start(va, format);
40*05767d91SRobert Wu         vsprintf(buffer, format, va);
41*05767d91SRobert Wu         ATrace_beginSection(buffer);
42*05767d91SRobert Wu         va_end(va);
43*05767d91SRobert Wu     } else {
44*05767d91SRobert Wu         LOGE("Tracing is either not initialized (call Trace::initialize()) "
45*05767d91SRobert Wu              "or not supported on this device");
46*05767d91SRobert Wu     }
47*05767d91SRobert Wu }
48*05767d91SRobert Wu 
endSection()49*05767d91SRobert Wu void Trace::endSection() {
50*05767d91SRobert Wu 
51*05767d91SRobert Wu     if (mIsTracingSupported) {
52*05767d91SRobert Wu         ATrace_endSection();
53*05767d91SRobert Wu     }
54*05767d91SRobert Wu }
55*05767d91SRobert Wu 
initialize()56*05767d91SRobert Wu void Trace::initialize() {
57*05767d91SRobert Wu 
58*05767d91SRobert Wu     // Using dlsym allows us to use tracing on API 21+ without needing android/trace.h which wasn't
59*05767d91SRobert Wu     // published until API 23
60*05767d91SRobert Wu     void *lib = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL);
61*05767d91SRobert Wu     if (lib == nullptr) {
62*05767d91SRobert Wu         LOGE("Could not open libandroid.so to dynamically load tracing symbols");
63*05767d91SRobert Wu     } else {
64*05767d91SRobert Wu         ATrace_beginSection =
65*05767d91SRobert Wu                 reinterpret_cast<fp_ATrace_beginSection >(
66*05767d91SRobert Wu                         dlsym(lib, "ATrace_beginSection"));
67*05767d91SRobert Wu         ATrace_endSection =
68*05767d91SRobert Wu                 reinterpret_cast<fp_ATrace_endSection >(
69*05767d91SRobert Wu                         dlsym(lib, "ATrace_endSection"));
70*05767d91SRobert Wu 
71*05767d91SRobert Wu         if (ATrace_beginSection != nullptr && ATrace_endSection != nullptr){
72*05767d91SRobert Wu             mIsTracingSupported = true;
73*05767d91SRobert Wu         }
74*05767d91SRobert Wu     }
75*05767d91SRobert Wu }
76