未验证 提交 f6d6df1c 编写于 作者: O openharmony_ci 提交者: Gitee

!483 通过系统属性进行linker log配置

Merge pull request !483 from dhy308/musl_log_sys_para
...@@ -47,10 +47,16 @@ if (is_lite_system && current_os == "ohos") { ...@@ -47,10 +47,16 @@ if (is_lite_system && current_os == "ohos") {
group("musl_libs") { group("musl_libs") {
deps = [ deps = [
":musl_headers", ":musl_headers",
":musl_sysparam",
":soft_libs", ":soft_libs",
] ]
} }
ohos_prebuilt_etc("musl_sysparam") {
source = "${musl_porting_dir}/etc/musl.para"
relative_install_dir = "param"
}
musl_libs("soft_libs") { musl_libs("soft_libs") {
} }
......
...@@ -53,7 +53,9 @@ declare_args() { ...@@ -53,7 +53,9 @@ declare_args() {
} }
declare_args() { declare_args() {
enable_musl_log = false if (!is_standard_system) {
enable_musl_log = false
}
musl_secure_level = 1 musl_secure_level = 1
} }
......
...@@ -1668,6 +1668,7 @@ musl_src_ldso = [ ...@@ -1668,6 +1668,7 @@ musl_src_ldso = [
"ldso/ns_config.c", "ldso/ns_config.c",
"ldso/strops.c", "ldso/strops.c",
"ldso/dynlink_rand.c", "ldso/dynlink_rand.c",
"ldso/ld_log.c",
] ]
if (musl_arch == "arm") { if (musl_arch == "arm") {
...@@ -2096,6 +2097,7 @@ musl_src_porting_file = [ ...@@ -2096,6 +2097,7 @@ musl_src_porting_file = [
"crt/aarch64/crti.s", "crt/aarch64/crti.s",
"crt/crtplus.c", "crt/crtplus.c",
"ldso/ld_log.h", "ldso/ld_log.h",
"ldso/ld_log.c",
"ldso/namespace.c", "ldso/namespace.c",
"ldso/ns_config.c", "ldso/ns_config.c",
"ldso/strops.c", "ldso/strops.c",
......
...@@ -149,8 +149,10 @@ template("musl_libs") { ...@@ -149,8 +149,10 @@ template("musl_libs") {
] ]
} }
if (enable_musl_log) { if (!is_standard_system && defined(enable_musl_log)) {
defines += [ "ENABLE_MUSL_LOG" ] if (enable_musl_log) {
defines += [ "ENABLE_MUSL_LOG" ]
}
} }
dynamic_list = dynamic_list =
...@@ -367,6 +369,11 @@ template("musl_libs") { ...@@ -367,6 +369,11 @@ template("musl_libs") {
} }
deps = porting_deps deps = porting_deps
if (is_standard_system) {
defines += [ "OHOS_ENABLE_PARAMETER" ]
deps += [ "//base/startup/init/services/param/base:parameterbase" ]
}
} }
source_set("soft_musl_src_nossp") { source_set("soft_musl_src_nossp") {
......
# Copyright (c) 2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
musl.log.enable=false
# ================================
# sample:
#
# * enable ld log for all apps
# musl.log.ld.all=true
#
# * enable ld log for specified app
# musl.log.ld.all=false
# musl.log.ld.app.your_app_name=true
#
# * enable ld log for apps excluding the specified app
# musl.log.ld.all=true
# musl.log.ld.app.your_app_name=false
# ================================
musl.log.ld.all=true
\ No newline at end of file
...@@ -483,7 +483,11 @@ static int check_vna_hash(Verdef *def, int16_t vsym, uint32_t vna_hash) ...@@ -483,7 +483,11 @@ static int check_vna_hash(Verdef *def, int16_t vsym, uint32_t vna_hash)
} }
verdef = (Verdef *)((char *)verdef + verdef->vd_next); verdef = (Verdef *)((char *)verdef + verdef->vd_next);
} }
#if (LD_LOG_LEVEL & LD_LOG_DEBUG)
if (!matched) {
LD_LOGD("check_vna_hash no matched found. vsym=%{public}d vna_hash=%{public}x", vsym, vna_hash);
}
#endif
return matched; return matched;
} }
...@@ -494,6 +498,8 @@ static int check_verinfo(Verdef *def, int16_t *versym, uint32_t index, struct ve ...@@ -494,6 +498,8 @@ static int check_verinfo(Verdef *def, int16_t *versym, uint32_t index, struct ve
if (strlen(verinfo->v) == 0) { if (strlen(verinfo->v) == 0) {
return 1; return 1;
} else { } else {
LD_LOGD("check_verinfo versym or def is null and verinfo->v exist, s:%{public}s v:%{public}s.",
verinfo->s, verinfo->v);
return 0; return 0;
} }
} }
...@@ -512,6 +518,7 @@ static int check_verinfo(Verdef *def, int16_t *versym, uint32_t index, struct ve ...@@ -512,6 +518,7 @@ static int check_verinfo(Verdef *def, int16_t *versym, uint32_t index, struct ve
if (vsym >= 0) { if (vsym >= 0) {
return 1; return 1;
} else { } else {
LD_LOGD("check_verinfo not default version. vsym:%{public}d s:%{public}s", vsym, verinfo->s);
return 0; return 0;
} }
} }
...@@ -530,7 +537,14 @@ static int check_verinfo(Verdef *def, int16_t *versym, uint32_t index, struct ve ...@@ -530,7 +537,14 @@ static int check_verinfo(Verdef *def, int16_t *versym, uint32_t index, struct ve
Verdaux *aux = (Verdaux *)((char *)def + def->vd_aux); Verdaux *aux = (Verdaux *)((char *)def + def->vd_aux);
return !strcmp(verinfo->v, strings + aux->vda_name); int ret = !strcmp(verinfo->v, strings + aux->vda_name);
#if (LD_LOG_LEVEL & LD_LOG_DEBUG)
if (!ret) {
LD_LOGD("check_verinfo version not match. s=%{public}s v=%{public}s vsym=%{public}d vda_name=%{public}s",
verinfo->s, verinfo->v, vsym, strings + aux->vda_name);
}
#endif
return ret;
} }
static uint32_t sysv_hash(const char *s0) static uint32_t sysv_hash(const char *s0)
...@@ -570,6 +584,9 @@ static Sym *sysv_lookup(struct verinfo *verinfo, uint32_t h, struct dso *dso) ...@@ -570,6 +584,9 @@ static Sym *sysv_lookup(struct verinfo *verinfo, uint32_t h, struct dso *dso)
} }
} }
LD_LOGD("sysv_lookup not find the symbol, "
"so:%{public}s s:%{public}s v:%{public}s use_vna_hash:%{public}d vna_hash:%{public}x",
dso->name, verinfo->s, verinfo->v, verinfo->use_vna_hash, verinfo->vna_hash);
return 0; return 0;
} }
...@@ -579,7 +596,10 @@ static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, struct v ...@@ -579,7 +596,10 @@ static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, struct v
uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4); uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
uint32_t i = buckets[h1 % nbuckets]; uint32_t i = buckets[h1 % nbuckets];
if (!i) return 0; if (!i) {
LD_LOGD("gnu_lookup symbol not found (bloom filter), so:%{public}s s:%{public}s", dso->name, verinfo->s);
return 0;
}
uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]); uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]);
...@@ -597,6 +617,9 @@ static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, struct v ...@@ -597,6 +617,9 @@ static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, struct v
if (h2 & 1) break; if (h2 & 1) break;
} }
LD_LOGD("gnu_lookup symbol not found, "
"so:%{public}s s:%{public}s v:%{public}s use_vna_hash:%{public}d vna_hash:%{public}x",
dso->name, verinfo->s, verinfo->v, verinfo->use_vna_hash, verinfo->vna_hash);
return 0; return 0;
} }
...@@ -616,6 +639,7 @@ static Sym *gnu_lookup_filtered(uint32_t h1, uint32_t *hashtab, struct dso *dso, ...@@ -616,6 +639,7 @@ static Sym *gnu_lookup_filtered(uint32_t h1, uint32_t *hashtab, struct dso *dso,
static bool check_sym_accessible(struct dso *dso, ns_t *ns) static bool check_sym_accessible(struct dso *dso, ns_t *ns)
{ {
if (!dso || !dso->namespace || !ns) { if (!dso || !dso->namespace || !ns) {
LD_LOGD("check_sym_accessible invalid parameter!");
return false; return false;
} }
if (dso->namespace == ns) { if (dso->namespace == ns) {
...@@ -626,6 +650,8 @@ static bool check_sym_accessible(struct dso *dso, ns_t *ns) ...@@ -626,6 +650,8 @@ static bool check_sym_accessible(struct dso *dso, ns_t *ns)
return true; return true;
} }
} }
LD_LOGD(
"check_sym_accessible dso name [%{public}s] ns_name [%{public}s] not accessible!", dso->name, ns->ns_name);
return false; return false;
} }
...@@ -840,6 +866,9 @@ static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stri ...@@ -840,6 +866,9 @@ static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stri
dso->lazy_cnt++; dso->lazy_cnt++;
continue; continue;
} }
LD_LOGE("relocating failed: symbol not found. "
"dso=%{public}s s=%{public}s use_vna_hash=%{public}d van_hash=%{public}x",
dso->name, name, vinfo.use_vna_hash, vinfo.vna_hash);
error("Error relocating %s: %s: symbol not found", error("Error relocating %s: %s: symbol not found",
dso->name, name); dso->name, name);
if (runtime) longjmp(*rtld_fail, 1); if (runtime) longjmp(*rtld_fail, 1);
...@@ -2309,6 +2338,8 @@ void __dls3(size_t *sp, size_t *auxv) ...@@ -2309,6 +2338,8 @@ void __dls3(size_t *sp, size_t *auxv)
#ifdef OHOS_ENABLE_PARAMETER #ifdef OHOS_ENABLE_PARAMETER
InitParameterClient(); InitParameterClient();
#endif #endif
musl_log_reset();
ld_log_reset();
/* If the main program was already loaded by the kernel, /* If the main program was already loaded by the kernel,
* AT_PHDR will point to some location other than the dynamic * AT_PHDR will point to some location other than the dynamic
* linker's program headers. */ * linker's program headers. */
...@@ -2860,6 +2891,8 @@ end: ...@@ -2860,6 +2891,8 @@ end:
void *dlopen(const char *file, int mode) void *dlopen(const char *file, int mode)
{ {
const void *caller_addr = __builtin_return_address(0); const void *caller_addr = __builtin_return_address(0);
musl_log_reset();
ld_log_reset();
LD_LOGI("dlopen file:%{public}s, mode:%{public}x ,caller_addr:%{public}p .", file, mode, caller_addr); LD_LOGI("dlopen file:%{public}s, mode:%{public}x ,caller_addr:%{public}p .", file, mode, caller_addr);
return dlopen_impl(file, mode, NULL, caller_addr, NULL); return dlopen_impl(file, mode, NULL, caller_addr, NULL);
} }
...@@ -2912,11 +2945,13 @@ int dlns_get(const char *name, Dl_namespace *dlns) ...@@ -2912,11 +2945,13 @@ int dlns_get(const char *name, Dl_namespace *dlns)
void *dlopen_ns(Dl_namespace *dlns, const char *file, int mode) void *dlopen_ns(Dl_namespace *dlns, const char *file, int mode)
{ {
const void *caller_addr = __builtin_return_address(0); const void *caller_addr = __builtin_return_address(0);
musl_log_reset();
ld_log_reset();
LD_LOGI("dlopen_ns file:%{public}s, mode:%{public}x , caller_addr:%{public}p , dlns->name:%{public}s.", LD_LOGI("dlopen_ns file:%{public}s, mode:%{public}x , caller_addr:%{public}p , dlns->name:%{public}s.",
file, file,
mode, mode,
caller_addr, caller_addr,
dlns->name); dlns ? dlns->name : "NULL");
return dlopen_impl(file, mode, dlns->name, caller_addr, NULL); return dlopen_impl(file, mode, dlns->name, caller_addr, NULL);
} }
...@@ -3082,6 +3117,7 @@ static void *do_dlsym(struct dso *p, const char *s, const char *v, void *ra) ...@@ -3082,6 +3117,7 @@ static void *do_dlsym(struct dso *p, const char *s, const char *v, void *ra)
struct verinfo verinfo = { .s = s, .v = v, .use_vna_hash = false }; struct verinfo verinfo = { .s = s, .v = v, .use_vna_hash = false };
struct symdef def = find_sym2(p, &verinfo, 0, use_deps, ns); struct symdef def = find_sym2(p, &verinfo, 0, use_deps, ns);
if (!def.sym) { if (!def.sym) {
LD_LOGE("do_dlsym failed: symbol not found. so=%{public}s s=%{public}s v=%{public}s", p->name, s, v);
error("Symbol not found: %s, version: %s", s, strlen(v) > 0 ? v : "null"); error("Symbol not found: %s, version: %s", s, strlen(v) > 0 ? v : "null");
return 0; return 0;
} }
...@@ -3300,6 +3336,8 @@ int dladdr(const void *addr_arg, Dl_info *info) ...@@ -3300,6 +3336,8 @@ int dladdr(const void *addr_arg, Dl_info *info)
hidden void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra) hidden void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra)
{ {
void *res; void *res;
musl_log_reset();
ld_log_reset();
pthread_rwlock_rdlock(&lock); pthread_rwlock_rdlock(&lock);
#ifdef HANDLE_RANDOMIZATION #ifdef HANDLE_RANDOMIZATION
if ((p != RTLD_DEFAULT) && (p != RTLD_NEXT)) { if ((p != RTLD_DEFAULT) && (p != RTLD_NEXT)) {
...@@ -3322,6 +3360,8 @@ hidden void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra ...@@ -3322,6 +3360,8 @@ hidden void *__dlsym(void *restrict p, const char *restrict s, void *restrict ra
hidden void *__dlvsym(void *restrict p, const char *restrict s, const char *restrict v, void *restrict ra) hidden void *__dlvsym(void *restrict p, const char *restrict s, const char *restrict v, void *restrict ra)
{ {
void *res; void *res;
musl_log_reset();
ld_log_reset();
pthread_rwlock_rdlock(&lock); pthread_rwlock_rdlock(&lock);
#ifdef HANDLE_RANDOMIZATION #ifdef HANDLE_RANDOMIZATION
if ((p != RTLD_DEFAULT) && (p != RTLD_NEXT)) { if ((p != RTLD_DEFAULT) && (p != RTLD_NEXT)) {
...@@ -3523,13 +3563,15 @@ int handle_asan_path_open(int fd, const char *name, ns_t *namespace, char *buf, ...@@ -3523,13 +3563,15 @@ int handle_asan_path_open(int fd, const char *name, ns_t *namespace, char *buf,
void* dlopen_ext(const char *file, int mode, const dl_extinfo *extinfo) void* dlopen_ext(const char *file, int mode, const dl_extinfo *extinfo)
{ {
const void *caller_addr = __builtin_return_address(0);
musl_log_reset();
ld_log_reset();
if (extinfo != NULL) { if (extinfo != NULL) {
if ((extinfo->flag & ~(DL_EXT_VALID_FLAG_BITS)) != 0) { if ((extinfo->flag & ~(DL_EXT_VALID_FLAG_BITS)) != 0) {
LD_LOGE("Error dlopen_ext %{public}s: invalid flag %{public}x", file, extinfo->flag); LD_LOGE("Error dlopen_ext %{public}s: invalid flag %{public}x", file, extinfo->flag);
return NULL; return NULL;
} }
} }
const void *caller_addr = __builtin_return_address(0);
LD_LOGI("dlopen_ext file:%{public}s, mode:%{public}x , caller_addr:%{public}p , extinfo->flag:%{public}x", LD_LOGI("dlopen_ext file:%{public}s, mode:%{public}x , caller_addr:%{public}p , extinfo->flag:%{public}x",
file, file,
mode, mode,
......
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ld_log.h"
static bool ld_log_enable = false;
#ifdef OHOS_ENABLE_PARAMETER
#include <fcntl.h>
#include <stdint.h>
#include <string.h>
#include <sys/prctl.h>
#include <unistd.h>
#include "sys_param.h"
#define SYSPARAM_LENGTH 32
#define PROCESS_NAME_LEN 1024
static char *get_app_name(char *buf, size_t length)
{
char *app = NULL;
int fd = open("/proc/self/cmdline", O_RDONLY);
if (fd != -1) {
ssize_t ret = read(fd, buf, length - 1);
if (ret != -1) {
buf[ret] = 0;
app = strrchr(buf, '/');
if (app) {
app++;
} else {
app = buf;
}
char *app_end = strchr(app, ':');
if (app_end) {
*app_end = 0;
}
}
close(fd);
}
return app;
}
static bool get_ld_log_app_value(char *buffer, uint32_t *length)
{
char buf[PROCESS_NAME_LEN];
char *path = get_app_name(buf, PROCESS_NAME_LEN);
if (!path) {
buffer[0] = 0;
return false;
}
char app_param_name[PROCESS_NAME_LEN] = "musl.log.ld.app.";
strcat(app_param_name, path);
if (SystemReadParam(app_param_name, buffer, length) == 0) {
buffer[*length] = 0;
return true;
}
buffer[0] = 0;
return false;
}
static bool get_ld_log_all_value(char *buffer, uint32_t *length)
{
if (SystemReadParam("musl.log.ld.all", buffer, length) == 0) {
buffer[*length] = 0;
return true;
}
buffer[0] = 0;
return false;
}
static inline void assign_ld_log_enable(char *param_value, const char *expect_value)
{
ld_log_enable = (strcmp(param_value, expect_value) == 0);
}
#endif
void ld_log_reset()
{
#if (defined(OHOS_ENABLE_PARAMETER))
if (!is_musl_log_enable()) {
ld_log_enable = false;
return;
}
char app_param_value[SYSPARAM_LENGTH];
uint32_t app_param_value_len = SYSPARAM_LENGTH;
char all_param_value[SYSPARAM_LENGTH];
uint32_t all_param_value_len = SYSPARAM_LENGTH;
if (get_ld_log_app_value(app_param_value, &app_param_value_len)) {
assign_ld_log_enable(app_param_value, "true");
} else {
if (get_ld_log_all_value(all_param_value, &all_param_value_len)) {
assign_ld_log_enable(all_param_value, "true");
} else {
ld_log_enable = false;
}
}
#else
ld_log_enable = is_musl_log_enable();
#endif
}
bool get_ld_log_enable()
{
return ld_log_enable;
}
...@@ -18,37 +18,56 @@ ...@@ -18,37 +18,56 @@
#include <musl_log.h> #include <musl_log.h>
#ifdef __cplusplus
extern "C" {
#endif
#define LD_LOG_ERROR 1 #define LD_LOG_ERROR 1
#define LD_LOG_WARNING 2 #define LD_LOG_WARNING 2
#define LD_LOG_INFO 4 #define LD_LOG_INFO 4
#define LD_LOG_DEBUG 8 #define LD_LOG_DEBUG 8
#define LD_LOG_LEVEL (LD_LOG_ERROR | LD_LOG_WARNING) #define LD_LOG_LEVEL (LD_LOG_ERROR | LD_LOG_WARNING | LD_LOG_INFO)
#define LD_LOG_TAG "MUSL-LDSO" #define LD_LOG_TAG "MUSL-LDSO"
#if (defined(ENABLE_MUSL_LOG) && (LD_LOG_LEVEL & LD_LOG_ERROR)) hidden bool get_ld_log_enable();
#define LD_LOGE(...) ((void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_ERROR, MUSL_LOG_DOMAIN, LD_LOG_TAG, __VA_ARGS__)) hidden void ld_log_reset();
#if ((LD_LOG_LEVEL & LD_LOG_ERROR) && (defined(OHOS_ENABLE_PARAMETER) || defined(ENABLE_MUSL_LOG)))
#define LD_LOGE(...) if (get_ld_log_enable()) { \
(void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_ERROR, MUSL_LOG_DOMAIN, LD_LOG_TAG, __VA_ARGS__); \
}
#else #else
#define LD_LOGE(...) #define LD_LOGE(...)
#endif #endif
#if (defined(ENABLE_MUSL_LOG) && (LD_LOG_LEVEL & LD_LOG_WARNING)) #if ((LD_LOG_LEVEL & LD_LOG_WARNING) && (defined(OHOS_ENABLE_PARAMETER) || defined(ENABLE_MUSL_LOG)))
#define LD_LOGW(...) ((void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_WARN, MUSL_LOG_DOMAIN, LD_LOG_TAG, __VA_ARGS__)) #define LD_LOGW(...) if (get_ld_log_enable()) { \
(void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_WARN, MUSL_LOG_DOMAIN, LD_LOG_TAG, __VA_ARGS__); \
}
#else #else
#define LD_LOGW(...) #define LD_LOGW(...)
#endif #endif
#if (defined(ENABLE_MUSL_LOG) && (LD_LOG_LEVEL & LD_LOG_INFO)) #if ((LD_LOG_LEVEL & LD_LOG_INFO) && (defined(OHOS_ENABLE_PARAMETER) || defined(ENABLE_MUSL_LOG)))
#define LD_LOGI(...) ((void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_INFO, MUSL_LOG_DOMAIN, LD_LOG_TAG, __VA_ARGS__)) #define LD_LOGI(...) if (get_ld_log_enable()) { \
(void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_INFO, MUSL_LOG_DOMAIN, LD_LOG_TAG, __VA_ARGS__); \
}
#else #else
#define LD_LOGI(...) #define LD_LOGI(...)
#endif #endif
#if (defined(ENABLE_MUSL_LOG) && (LD_LOG_LEVEL & LD_LOG_DEBUG)) #if (LD_LOG_LEVEL & LD_LOG_DEBUG) && (defined(OHOS_ENABLE_PARAMETER) || defined(ENABLE_MUSL_LOG))
#define LD_LOGD(...) ((void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_DEBUG, MUSL_LOG_DOMAIN, LD_LOG_TAG, __VA_ARGS__)) #define LD_LOGD(...) if (get_ld_log_enable()) { \
(void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_DEBUG, MUSL_LOG_DOMAIN, LD_LOG_TAG, __VA_ARGS__); \
}
#else #else
#define LD_LOGD(...) #define LD_LOGD(...)
#endif #endif
#ifdef __cplusplus
}
#endif
#endif // LD_LOG_H #endif // LD_LOG_H
...@@ -16,27 +16,40 @@ ...@@ -16,27 +16,40 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <hilog_adapter.h> #include <hilog_adapter.h>
#include "hilog_common.h"
#include "vsnprintf_s_p.h"
#include <errno.h> #include <errno.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <time.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/uio.h> #include <sys/uio.h>
#include <sys/un.h> #include <sys/un.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include "hilog_common.h"
#ifdef OHOS_ENABLE_PARAMETER
#include "sys_param.h"
#endif
#include "vsnprintf_s_p.h"
#define LOG_LEN 3 #define LOG_LEN 3
#define ERROR_FD 2 #define ERROR_FD 2
#ifdef OHOS_ENABLE_PARAMETER
#define SYSPARAM_LENGTH 32
#endif
const int SOCKET_TYPE = SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC; const int SOCKET_TYPE = SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC;
const int INVALID_SOCKET = -1; const int INVALID_SOCKET = -1;
const struct sockaddr_un SOCKET_ADDR = {AF_UNIX, SOCKET_FILE_DIR INPUT_SOCKET_NAME}; const struct sockaddr_un SOCKET_ADDR = {AF_UNIX, SOCKET_FILE_DIR INPUT_SOCKET_NAME};
static bool musl_log_enable = false;
#ifdef OHOS_ENABLE_PARAMETER
static const char *param_name = "musl.log.enable";
#endif
static int SendMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const char *fmt, uint16_t fmtLen) static int SendMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const char *fmt, uint16_t fmtLen)
{ {
int socketFd = TEMP_FAILURE_RETRY(socket(AF_UNIX, SOCKET_TYPE, 0)); int socketFd = TEMP_FAILURE_RETRY(socket(AF_UNIX, SOCKET_TYPE, 0));
...@@ -81,10 +94,6 @@ static int SendMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const ...@@ -81,10 +94,6 @@ static int SendMessage(HilogMsg *header, const char *tag, uint16_t tagLen, const
static int HiLogAdapterPrintArgs( static int HiLogAdapterPrintArgs(
const LogType type, const LogLevel level, const unsigned int domain, const char *tag, const char *fmt, va_list ap) const LogType type, const LogLevel level, const unsigned int domain, const char *tag, const char *fmt, va_list ap)
{ {
if (!HiLogAdapterIsLoggable(domain, tag, level)) {
return -1;
}
char buf[MAX_LOG_LEN] = {0}; char buf[MAX_LOG_LEN] = {0};
vsnprintfp_s(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, true, fmt, ap); vsnprintfp_s(buf, MAX_LOG_LEN, MAX_LOG_LEN - 1, true, fmt, ap);
...@@ -105,6 +114,10 @@ static int HiLogAdapterPrintArgs( ...@@ -105,6 +114,10 @@ static int HiLogAdapterPrintArgs(
int HiLogAdapterPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...) int HiLogAdapterPrint(LogType type, LogLevel level, unsigned int domain, const char *tag, const char *fmt, ...)
{ {
if (!HiLogAdapterIsLoggable(domain, tag, level)) {
return -1;
}
int ret; int ret;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
...@@ -113,10 +126,40 @@ int HiLogAdapterPrint(LogType type, LogLevel level, unsigned int domain, const c ...@@ -113,10 +126,40 @@ int HiLogAdapterPrint(LogType type, LogLevel level, unsigned int domain, const c
return ret; return ret;
} }
bool is_musl_log_enable()
{
if (getpid() == 1) {
return false;
}
return musl_log_enable;
}
bool HiLogAdapterIsLoggable(unsigned int domain, const char *tag, LogLevel level) bool HiLogAdapterIsLoggable(unsigned int domain, const char *tag, LogLevel level)
{ {
if (!is_musl_log_enable()) {
return false;
}
if ((level <= LOG_LEVEL_MIN) || (level >= LOG_LEVEL_MAX) || tag == NULL) { if ((level <= LOG_LEVEL_MIN) || (level >= LOG_LEVEL_MAX) || tag == NULL) {
return false; return false;
} }
return true; return true;
} }
void musl_log_reset()
{
#if (defined(OHOS_ENABLE_PARAMETER))
char param_value[SYSPARAM_LENGTH];
uint32_t length = SYSPARAM_LENGTH;
if (SystemReadParam(param_name, param_value, &length) == 0) {
param_value[length] = 0;
if (strcmp(param_value, "true") == 0) {
musl_log_enable = true;
return;
}
}
musl_log_enable = false;
#elif (defined(ENABLE_MUSL_LOG))
musl_log_enable = true;
#endif
}
...@@ -47,4 +47,8 @@ hidden int HiLogAdapterPrint(LogType type, LogLevel level, unsigned int domain, ...@@ -47,4 +47,8 @@ hidden int HiLogAdapterPrint(LogType type, LogLevel level, unsigned int domain,
__attribute__((__format__(os_log, 5, 6))); __attribute__((__format__(os_log, 5, 6)));
hidden bool HiLogAdapterIsLoggable(unsigned int domain, const char *tag, LogLevel level); hidden bool HiLogAdapterIsLoggable(unsigned int domain, const char *tag, LogLevel level);
hidden bool is_musl_log_enable();
hidden void musl_log_reset();
#endif // MUSL_HILOG_ADAPTER_H #endif // MUSL_HILOG_ADAPTER_H
...@@ -26,7 +26,7 @@ extern "C" { ...@@ -26,7 +26,7 @@ extern "C" {
#define MUSL_LOG_DOMAIN 0xD003F00 #define MUSL_LOG_DOMAIN 0xD003F00
#define MUSL_LOG_TAG "MUSL" #define MUSL_LOG_TAG "MUSL"
#ifdef ENABLE_MUSL_LOG #if (defined(OHOS_ENABLE_PARAMETER) || defined(ENABLE_MUSL_LOG))
#define MUSL_LOGE(...) ((void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_ERROR, MUSL_LOG_DOMAIN, MUSL_LOG_TAG, __VA_ARGS__)) #define MUSL_LOGE(...) ((void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_ERROR, MUSL_LOG_DOMAIN, MUSL_LOG_TAG, __VA_ARGS__))
#define MUSL_LOGW(...) ((void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_WARN, MUSL_LOG_DOMAIN, MUSL_LOG_TAG, __VA_ARGS__)) #define MUSL_LOGW(...) ((void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_WARN, MUSL_LOG_DOMAIN, MUSL_LOG_TAG, __VA_ARGS__))
#define MUSL_LOGI(...) ((void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_INFO, MUSL_LOG_DOMAIN, MUSL_LOG_TAG, __VA_ARGS__)) #define MUSL_LOGI(...) ((void)HiLogAdapterPrint(MUSL_LOG_TYPE, LOG_INFO, MUSL_LOG_DOMAIN, MUSL_LOG_TAG, __VA_ARGS__))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册