1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved. 3 4 * The bpf_log_writer.h is dual licensed: you can use it either under the terms of 5 * the GPL V2, or the 3-Clause BSD license, at your option. 6 * See the LICENSE file in the root of this repository for complete details. 7 */ 8 #ifndef BPF_LOG_WRITER_H 9 #define BPF_LOG_WRITER_H 10 11 #include "bpf_log.h" 12 #include "fstrace_types.h" 13 14 #define BPFLOG(level, expression, format, ...) { \ 15 u32 bpf_log_level_index = BPF_LOG_LEVEL_INDEX; \ 16 u32* bpf_log_level_ptr = bpf_map_lookup_elem(&config_var_map, &bpf_log_level_index); \ 17 if (bpf_log_level_ptr) { \ 18 u32 bpf_log_level = BPF_LOG_NONE; \ 19 bpf_probe_read_kernel(&bpf_log_level, sizeof(u32), bpf_log_level_ptr); \ 20 if ((expression) && (bpf_log_level <= level)) { \ 21 char mesg[] = format; \ 22 bpf_trace_printk(mesg, sizeof(mesg), ##__VA_ARGS__); \ 23 } \ 24 } else { \ 25 char mesg[] = "failed to get bpf log level"; \ 26 bpf_trace_printk(mesg, sizeof(mesg)); \ 27 } \ 28 } 29 30 #ifdef BPF_LOGGER_DEBUG 31 #define BPFLOGD(expression, format, ...) BPFLOG(BPF_LOG_DEBUG, expression, format, ##__VA_ARGS__) 32 #define BPFLOGI(expression, format, ...) BPFLOG(BPF_LOG_INFO, expression, format, ##__VA_ARGS__) 33 #define BPFLOGW(expression, format, ...) BPFLOG(BPF_LOG_WARN, expression, format, ##__VA_ARGS__) 34 #define BPFLOGE(expression, format, ...) BPFLOG(BPF_LOG_ERROR, expression, format, ##__VA_ARGS__) 35 #define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__) 36 37 #elif defined(BPF_LOGGER_INFO) 38 #define BPFLOGD(expression, format, ...) {} 39 #define BPFLOGI(expression, format, ...) BPFLOG(BPF_LOG_INFO, expression, format, ##__VA_ARGS__) 40 #define BPFLOGW(expression, format, ...) BPFLOG(BPF_LOG_WARN, expression, format, ##__VA_ARGS__) 41 #define BPFLOGE(expression, format, ...) BPFLOG(BPF_LOG_ERROR, expression, format, ##__VA_ARGS__) 42 #define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__) 43 44 #elif defined(BPF_LOGGER_WARN) 45 #define BPFLOGD(expression, format, ...) {} 46 #define BPFLOGI(expression, format, ...) {} 47 #define BPFLOGW(expression, format, ...) BPFLOG(BPF_LOG_WARN, expression, format, ##__VA_ARGS__) 48 #define BPFLOGE(expression, format, ...) BPFLOG(BPF_LOG_ERROR, expression, format, ##__VA_ARGS__) 49 #define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__) 50 51 #elif defined(BPF_LOGGER_ERROR) 52 #define BPFLOGD(expression, format, ...) {} 53 #define BPFLOGI(expression, format, ...) {} 54 #define BPFLOGW(expression, format, ...) {} 55 #define BPFLOGE(expression, format, ...) BPFLOG(BPF_LOG_ERROR, expression, format, ##__VA_ARGS__) 56 #define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__) 57 58 #elif defined(BPF_LOGGER_FATAL) 59 #define BPFLOGD(expression, format, ...) {} 60 #define BPFLOGI(expression, format, ...) {} 61 #define BPFLOGW(expression, format, ...) {} 62 #define BPFLOGE(expression, format, ...) {} 63 #define BPFLOGF(expression, format, ...) BPFLOG(BPF_LOG_FATAL, expression, format, ##__VA_ARGS__) 64 65 #else 66 #define BPFLOGD(expression, format, ...) {} 67 #define BPFLOGI(expression, format, ...) {} 68 #define BPFLOGW(expression, format, ...) {} 69 #define BPFLOGE(expression, format, ...) {} 70 #define BPFLOGF(expression, format, ...) {} 71 72 #endif // BPF_LOGGER_LEVEL 73 74 #endif // BPF_LOG_WRITER_H