blob: 2e4ab3e8e1f58dcfe8e11712f20fd1e42749be6c [file] [log] [blame]
Tom Cherry1bfa8ab2020-01-08 14:47:42 -08001/*
2 * Copyright (C) 2020 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 "liblog_symbols.h"
18
Dan Albert361fdad2020-11-17 15:31:05 -080019// __ANDROID_APEX_MIN_SDK_VERSION__ is the max of all the minSdkVersions that
20// this module shares an APEX with. In other words, this is the lowest OS
21// version that this code could possibly run on.
22#if defined(__ANDROID_APEX_MIN_SDK_VERSION__) && (__ANDROID_APEX_MIN_SDK_VERSION__ <= 29)
23#define USE_DLSYM
24#endif
25
26// __ANDROID_API__ is the historical name for __ANDROID_MIN_SDK_VERSION__. The
27// latter is not set by the Clang we're currently using. This path will be used
28// when this code is built by NDK modules rather than APEX modules.
29#if defined(__ANDROID_API__) && (__ANDROID_API__ <= 29)
Jiyong Park76b52db2020-03-03 16:12:36 +090030#define USE_DLSYM
31#endif
Jiyong Park76b52db2020-03-03 16:12:36 +090032
33#ifdef USE_DLSYM
Tom Cherry1bfa8ab2020-01-08 14:47:42 -080034#include <dlfcn.h>
35#endif
36
37namespace android {
38namespace base {
39
Jiyong Park76b52db2020-03-03 16:12:36 +090040#ifdef USE_DLSYM
Tom Cherry1bfa8ab2020-01-08 14:47:42 -080041
42const std::optional<LibLogFunctions>& GetLibLogFunctions() {
43 static std::optional<LibLogFunctions> liblog_functions = []() -> std::optional<LibLogFunctions> {
44 void* liblog_handle = dlopen("liblog.so", RTLD_NOW);
45 if (liblog_handle == nullptr) {
46 return {};
47 }
48
49 LibLogFunctions real_liblog_functions = {};
50
51#define DLSYM(name) \
52 real_liblog_functions.name = \
53 reinterpret_cast<decltype(LibLogFunctions::name)>(dlsym(liblog_handle, #name)); \
54 if (real_liblog_functions.name == nullptr) { \
55 return {}; \
56 }
57
58 DLSYM(__android_log_set_logger)
Tom Cherry4ab46e92020-03-11 11:07:13 -070059 DLSYM(__android_log_write_log_message)
Tom Cherry1bfa8ab2020-01-08 14:47:42 -080060 DLSYM(__android_log_logd_logger)
61 DLSYM(__android_log_stderr_logger)
62 DLSYM(__android_log_set_aborter)
63 DLSYM(__android_log_call_aborter)
64 DLSYM(__android_log_default_aborter)
Tom Cherry4916e092020-01-16 15:58:02 -080065 DLSYM(__android_log_set_minimum_priority);
66 DLSYM(__android_log_get_minimum_priority);
Tom Cherrydbc07ce2020-01-22 07:48:42 -080067 DLSYM(__android_log_set_default_tag);
Tom Cherry1bfa8ab2020-01-08 14:47:42 -080068#undef DLSYM
69
70 return real_liblog_functions;
71 }();
72
73 return liblog_functions;
74}
75
76#else
77
78const std::optional<LibLogFunctions>& GetLibLogFunctions() {
79 static std::optional<LibLogFunctions> liblog_functions = []() -> std::optional<LibLogFunctions> {
80 return LibLogFunctions{
81 .__android_log_set_logger = __android_log_set_logger,
Tom Cherry4ab46e92020-03-11 11:07:13 -070082 .__android_log_write_log_message = __android_log_write_log_message,
Tom Cherry1bfa8ab2020-01-08 14:47:42 -080083 .__android_log_logd_logger = __android_log_logd_logger,
84 .__android_log_stderr_logger = __android_log_stderr_logger,
85 .__android_log_set_aborter = __android_log_set_aborter,
86 .__android_log_call_aborter = __android_log_call_aborter,
87 .__android_log_default_aborter = __android_log_default_aborter,
Tom Cherry4916e092020-01-16 15:58:02 -080088 .__android_log_set_minimum_priority = __android_log_set_minimum_priority,
89 .__android_log_get_minimum_priority = __android_log_get_minimum_priority,
Tom Cherrydbc07ce2020-01-22 07:48:42 -080090 .__android_log_set_default_tag = __android_log_set_default_tag,
Tom Cherry1bfa8ab2020-01-08 14:47:42 -080091 };
92 }();
93 return liblog_functions;
94}
95
96#endif
97
98} // namespace base
99} // namespace android