Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 1 | /* |
| 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 Albert | 361fdad | 2020-11-17 15:31:05 -0800 | [diff] [blame^] | 19 | // __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 Park | 76b52db | 2020-03-03 16:12:36 +0900 | [diff] [blame] | 30 | #define USE_DLSYM |
| 31 | #endif |
Jiyong Park | 76b52db | 2020-03-03 16:12:36 +0900 | [diff] [blame] | 32 | |
| 33 | #ifdef USE_DLSYM |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 34 | #include <dlfcn.h> |
| 35 | #endif |
| 36 | |
| 37 | namespace android { |
| 38 | namespace base { |
| 39 | |
Jiyong Park | 76b52db | 2020-03-03 16:12:36 +0900 | [diff] [blame] | 40 | #ifdef USE_DLSYM |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 41 | |
| 42 | const 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 Cherry | 4ab46e9 | 2020-03-11 11:07:13 -0700 | [diff] [blame] | 59 | DLSYM(__android_log_write_log_message) |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 60 | 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 Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 65 | DLSYM(__android_log_set_minimum_priority); |
| 66 | DLSYM(__android_log_get_minimum_priority); |
Tom Cherry | dbc07ce | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 67 | DLSYM(__android_log_set_default_tag); |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 68 | #undef DLSYM |
| 69 | |
| 70 | return real_liblog_functions; |
| 71 | }(); |
| 72 | |
| 73 | return liblog_functions; |
| 74 | } |
| 75 | |
| 76 | #else |
| 77 | |
| 78 | const 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 Cherry | 4ab46e9 | 2020-03-11 11:07:13 -0700 | [diff] [blame] | 82 | .__android_log_write_log_message = __android_log_write_log_message, |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 83 | .__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 Cherry | 4916e09 | 2020-01-16 15:58:02 -0800 | [diff] [blame] | 88 | .__android_log_set_minimum_priority = __android_log_set_minimum_priority, |
| 89 | .__android_log_get_minimum_priority = __android_log_get_minimum_priority, |
Tom Cherry | dbc07ce | 2020-01-22 07:48:42 -0800 | [diff] [blame] | 90 | .__android_log_set_default_tag = __android_log_set_default_tag, |
Tom Cherry | 1bfa8ab | 2020-01-08 14:47:42 -0800 | [diff] [blame] | 91 | }; |
| 92 | }(); |
| 93 | return liblog_functions; |
| 94 | } |
| 95 | |
| 96 | #endif |
| 97 | |
| 98 | } // namespace base |
| 99 | } // namespace android |