Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2022, Oracle and/or its affiliates. */
0003 #include "bpf_iter.h"
0004 #include <bpf/bpf_helpers.h>
0005 
0006 char _license[] SEC("license") = "GPL";
0007 
0008 unsigned long last_sym_value = 0;
0009 
0010 static inline char tolower(char c)
0011 {
0012     if (c >= 'A' && c <= 'Z')
0013         c += ('a' - 'A');
0014     return c;
0015 }
0016 
0017 static inline char toupper(char c)
0018 {
0019     if (c >= 'a' && c <= 'z')
0020         c -= ('a' - 'A');
0021     return c;
0022 }
0023 
0024 /* Dump symbols with max size; the latter is calculated by caching symbol N value
0025  * and when iterating on symbol N+1, we can print max size of symbol N via
0026  * address of N+1 - address of N.
0027  */
0028 SEC("iter/ksym")
0029 int dump_ksym(struct bpf_iter__ksym *ctx)
0030 {
0031     struct seq_file *seq = ctx->meta->seq;
0032     struct kallsym_iter *iter = ctx->ksym;
0033     __u32 seq_num = ctx->meta->seq_num;
0034     unsigned long value;
0035     char type;
0036     int ret;
0037 
0038     if (!iter)
0039         return 0;
0040 
0041     if (seq_num == 0) {
0042         BPF_SEQ_PRINTF(seq, "ADDR TYPE NAME MODULE_NAME KIND MAX_SIZE\n");
0043         return 0;
0044     }
0045     if (last_sym_value)
0046         BPF_SEQ_PRINTF(seq, "0x%x\n", iter->value - last_sym_value);
0047     else
0048         BPF_SEQ_PRINTF(seq, "\n");
0049 
0050     value = iter->show_value ? iter->value : 0;
0051 
0052     last_sym_value = value;
0053 
0054     type = iter->type;
0055 
0056     if (iter->module_name[0]) {
0057         type = iter->exported ? toupper(type) : tolower(type);
0058         BPF_SEQ_PRINTF(seq, "0x%llx %c %s [ %s ] ",
0059                    value, type, iter->name, iter->module_name);
0060     } else {
0061         BPF_SEQ_PRINTF(seq, "0x%llx %c %s ", value, type, iter->name);
0062     }
0063     if (!iter->pos_arch_end || iter->pos_arch_end > iter->pos)
0064         BPF_SEQ_PRINTF(seq, "CORE ");
0065     else if (!iter->pos_mod_end || iter->pos_mod_end > iter->pos)
0066         BPF_SEQ_PRINTF(seq, "MOD ");
0067     else if (!iter->pos_ftrace_mod_end || iter->pos_ftrace_mod_end > iter->pos)
0068         BPF_SEQ_PRINTF(seq, "FTRACE_MOD ");
0069     else if (!iter->pos_bpf_end || iter->pos_bpf_end > iter->pos)
0070         BPF_SEQ_PRINTF(seq, "BPF ");
0071     else
0072         BPF_SEQ_PRINTF(seq, "KPROBE ");
0073     return 0;
0074 }