Linux Audio

Check our new training course

Loading...
v6.2
 1// SPDX-License-Identifier: GPL-2.0
 2#include "symbol/kallsyms.h"
 3#include "api/io.h"
 4#include <stdio.h>
 5#include <sys/stat.h>
 6#include <fcntl.h>
 7
 8u8 kallsyms2elf_type(char type)
 9{
10	type = tolower(type);
11	return (type == 't' || type == 'w') ? STT_FUNC : STT_OBJECT;
12}
13
14bool kallsyms__is_function(char symbol_type)
15{
16	symbol_type = toupper(symbol_type);
17	return symbol_type == 'T' || symbol_type == 'W';
18}
19
20static void read_to_eol(struct io *io)
21{
22	int ch;
23
24	for (;;) {
25		ch = io__get_char(io);
26		if (ch < 0 || ch == '\n')
27			return;
28	}
29}
30
31int kallsyms__parse(const char *filename, void *arg,
32		    int (*process_symbol)(void *arg, const char *name,
33					  char type, u64 start))
34{
35	struct io io;
36	char bf[BUFSIZ];
37	int err;
38
39	io.fd = open(filename, O_RDONLY, 0);
40
41	if (io.fd < 0)
42		return -1;
43
44	io__init(&io, io.fd, bf, sizeof(bf));
45
46	err = 0;
47	while (!io.eof) {
48		__u64 start;
49		int ch;
50		size_t i;
51		char symbol_type;
52		char symbol_name[KSYM_NAME_LEN + 1];
53
54		if (io__get_hex(&io, &start) != ' ') {
55			read_to_eol(&io);
56			continue;
57		}
58		symbol_type = io__get_char(&io);
59		if (io__get_char(&io) != ' ') {
60			read_to_eol(&io);
 
 
 
61			continue;
 
 
 
 
 
 
 
 
 
62		}
63		for (i = 0; i < sizeof(symbol_name); i++) {
64			ch = io__get_char(&io);
65			if (ch < 0 || ch == '\n')
66				break;
67			symbol_name[i]  = ch;
68		}
69		symbol_name[i]  = '\0';
70
71		err = process_symbol(arg, symbol_name, symbol_type, start);
72		if (err)
73			break;
74	}
75
76	close(io.fd);
 
77	return err;
 
 
 
78}
v4.10.11
 
 1#include "symbol/kallsyms.h"
 
 2#include <stdio.h>
 3#include <stdlib.h>
 
 4
 5u8 kallsyms2elf_type(char type)
 6{
 7	type = tolower(type);
 8	return (type == 't' || type == 'w') ? STT_FUNC : STT_OBJECT;
 9}
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11int kallsyms__parse(const char *filename, void *arg,
12		    int (*process_symbol)(void *arg, const char *name,
13					  char type, u64 start))
14{
15	char *line = NULL;
16	size_t n;
17	int err = -1;
18	FILE *file = fopen(filename, "r");
 
19
20	if (file == NULL)
21		goto out_failure;
 
 
22
23	err = 0;
24
25	while (!feof(file)) {
26		u64 start;
27		int line_len, len;
28		char symbol_type;
29		char *symbol_name;
30
31		line_len = getline(&line, &n, file);
32		if (line_len < 0 || !line)
33			break;
34
35		line[--line_len] = '\0'; /* \n */
36
37		len = hex2u64(line, &start);
38
39		len++;
40		if (len + 2 >= line_len)
41			continue;
42
43		symbol_type = line[len];
44		len += 2;
45		symbol_name = line + len;
46		len = line_len - len;
47
48		if (len >= KSYM_NAME_LEN) {
49			err = -1;
50			break;
51		}
 
 
 
 
 
 
 
52
53		err = process_symbol(arg, symbol_name, symbol_type, start);
54		if (err)
55			break;
56	}
57
58	free(line);
59	fclose(file);
60	return err;
61
62out_failure:
63	return -1;
64}