• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 <dlfcn.h>
18 #include <cstdio>
19 #include "Trace.h"
20 #include "OboeDebug.h"
21 
22 static char buffer[256];
23 
24 // Tracing functions
25 static void *(*ATrace_beginSection)(const char *sectionName);
26 
27 static void *(*ATrace_endSection)();
28 
29 typedef void *(*fp_ATrace_beginSection)(const char *sectionName);
30 
31 typedef void *(*fp_ATrace_endSection)();
32 
33 bool Trace::mIsTracingSupported = false;
34 
beginSection(const char * format,...)35 void Trace::beginSection(const char *format, ...){
36 
37     if (mIsTracingSupported) {
38         va_list va;
39         va_start(va, format);
40         vsprintf(buffer, format, va);
41         ATrace_beginSection(buffer);
42         va_end(va);
43     } else {
44         LOGE("Tracing is either not initialized (call Trace::initialize()) "
45              "or not supported on this device");
46     }
47 }
48 
endSection()49 void Trace::endSection() {
50 
51     if (mIsTracingSupported) {
52         ATrace_endSection();
53     }
54 }
55 
initialize()56 void Trace::initialize() {
57 
58     // Using dlsym allows us to use tracing on API 21+ without needing android/trace.h which wasn't
59     // published until API 23
60     void *lib = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL);
61     if (lib == nullptr) {
62         LOGE("Could not open libandroid.so to dynamically load tracing symbols");
63     } else {
64         ATrace_beginSection =
65                 reinterpret_cast<fp_ATrace_beginSection >(
66                         dlsym(lib, "ATrace_beginSection"));
67         ATrace_endSection =
68                 reinterpret_cast<fp_ATrace_endSection >(
69                         dlsym(lib, "ATrace_endSection"));
70 
71         if (ATrace_beginSection != nullptr && ATrace_endSection != nullptr){
72             mIsTracingSupported = true;
73         }
74     }
75 }