Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include "symbol/kallsyms.h"
0003 #include "api/io.h"
0004 #include <stdio.h>
0005 #include <sys/stat.h>
0006 #include <fcntl.h>
0007 
0008 u8 kallsyms2elf_type(char type)
0009 {
0010     type = tolower(type);
0011     return (type == 't' || type == 'w') ? STT_FUNC : STT_OBJECT;
0012 }
0013 
0014 bool kallsyms__is_function(char symbol_type)
0015 {
0016     symbol_type = toupper(symbol_type);
0017     return symbol_type == 'T' || symbol_type == 'W';
0018 }
0019 
0020 static void read_to_eol(struct io *io)
0021 {
0022     int ch;
0023 
0024     for (;;) {
0025         ch = io__get_char(io);
0026         if (ch < 0 || ch == '\n')
0027             return;
0028     }
0029 }
0030 
0031 int kallsyms__parse(const char *filename, void *arg,
0032             int (*process_symbol)(void *arg, const char *name,
0033                       char type, u64 start))
0034 {
0035     struct io io;
0036     char bf[BUFSIZ];
0037     int err;
0038 
0039     io.fd = open(filename, O_RDONLY, 0);
0040 
0041     if (io.fd < 0)
0042         return -1;
0043 
0044     io__init(&io, io.fd, bf, sizeof(bf));
0045 
0046     err = 0;
0047     while (!io.eof) {
0048         __u64 start;
0049         int ch;
0050         size_t i;
0051         char symbol_type;
0052         char symbol_name[KSYM_NAME_LEN + 1];
0053 
0054         if (io__get_hex(&io, &start) != ' ') {
0055             read_to_eol(&io);
0056             continue;
0057         }
0058         symbol_type = io__get_char(&io);
0059         if (io__get_char(&io) != ' ') {
0060             read_to_eol(&io);
0061             continue;
0062         }
0063         for (i = 0; i < sizeof(symbol_name); i++) {
0064             ch = io__get_char(&io);
0065             if (ch < 0 || ch == '\n')
0066                 break;
0067             symbol_name[i]  = ch;
0068         }
0069         symbol_name[i]  = '\0';
0070 
0071         err = process_symbol(arg, symbol_name, symbol_type, start);
0072         if (err)
0073             break;
0074     }
0075 
0076     close(io.fd);
0077     return err;
0078 }