• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 "src/android_internal/lazy_library_loader.h"
18 
19 #include <dlfcn.h>
20 #include <stdlib.h>
21 
22 #include "perfetto/base/build_config.h"
23 #include "perfetto/base/logging.h"
24 
25 namespace perfetto {
26 namespace android_internal {
27 
28 namespace {
29 
30 const char kLibName[] = "libperfetto_android_internal.so";
31 
LoadLibraryOnce()32 void* LoadLibraryOnce() {
33 #if !PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
34   // For testing only. Allows to use the version of the .so shipped in the
35   // system (if any) with the standalone builds of perfetto. This is really
36   // crash-prone and should not be used in production. The .so doesn't have a
37   // stable ABI, hence the version of the library in the system and the code in
38   // ToT can diverge.
39   const char* env_var = getenv("PERFETTO_ENABLE_ANDROID_INTERNAL_LIB");
40   if (!env_var || strcmp(env_var, "1")) {
41     PERFETTO_ELOG(
42         "android_internal functions can be used only with in-tree builds of "
43         "perfetto.");
44     return nullptr;
45   }
46 #endif
47   void* handle = dlopen(kLibName, RTLD_NOW);
48   if (!handle)
49     PERFETTO_PLOG("dlopen(%s) failed", kLibName);
50   return handle;
51 }
52 
53 }  // namespace
54 
LazyLoadFunction(const char * name)55 void* LazyLoadFunction(const char* name) {
56   // Strip the namespace qualification from the full symbol name.
57   const char* sep = strrchr(name, ':');
58   const char* function_name = sep ? sep + 1 : name;
59   static void* handle = LoadLibraryOnce();
60   if (!handle)
61     return nullptr;
62   void* fn = dlsym(handle, function_name);
63   if (!fn)
64     PERFETTO_PLOG("dlsym(%s) failed", function_name);
65   return fn;
66 }
67 
68 }  // namespace android_internal
69 }  // namespace perfetto
70