xref: /openbmc/linux/tools/perf/util/syscalltbl.c (revision 727dede0)
1 /*
2  * System call table mapper
3  *
4  * (C) 2016 Arnaldo Carvalho de Melo <acme@redhat.com>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  */
15 
16 #include "syscalltbl.h"
17 #include <stdlib.h>
18 #include <linux/compiler.h>
19 
20 #ifdef HAVE_SYSCALL_TABLE
21 #include <string.h>
22 #include "string2.h"
23 #include "util.h"
24 
25 #if defined(__x86_64__)
26 #include <asm/syscalls_64.c>
27 const int syscalltbl_native_max_id = SYSCALLTBL_x86_64_MAX_ID;
28 static const char **syscalltbl_native = syscalltbl_x86_64;
29 #endif
30 
31 struct syscall {
32 	int id;
33 	const char *name;
34 };
35 
36 static int syscallcmpname(const void *vkey, const void *ventry)
37 {
38 	const char *key = vkey;
39 	const struct syscall *entry = ventry;
40 
41 	return strcmp(key, entry->name);
42 }
43 
44 static int syscallcmp(const void *va, const void *vb)
45 {
46 	const struct syscall *a = va, *b = vb;
47 
48 	return strcmp(a->name, b->name);
49 }
50 
51 static int syscalltbl__init_native(struct syscalltbl *tbl)
52 {
53 	int nr_entries = 0, i, j;
54 	struct syscall *entries;
55 
56 	for (i = 0; i <= syscalltbl_native_max_id; ++i)
57 		if (syscalltbl_native[i])
58 			++nr_entries;
59 
60 	entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries);
61 	if (tbl->syscalls.entries == NULL)
62 		return -1;
63 
64 	for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) {
65 		if (syscalltbl_native[i]) {
66 			entries[j].name = syscalltbl_native[i];
67 			entries[j].id = i;
68 			++j;
69 		}
70 	}
71 
72 	qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp);
73 	tbl->syscalls.nr_entries = nr_entries;
74 	return 0;
75 }
76 
77 struct syscalltbl *syscalltbl__new(void)
78 {
79 	struct syscalltbl *tbl = malloc(sizeof(*tbl));
80 	if (tbl) {
81 		if (syscalltbl__init_native(tbl)) {
82 			free(tbl);
83 			return NULL;
84 		}
85 	}
86 	return tbl;
87 }
88 
89 void syscalltbl__delete(struct syscalltbl *tbl)
90 {
91 	zfree(&tbl->syscalls.entries);
92 	free(tbl);
93 }
94 
95 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id)
96 {
97 	return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL;
98 }
99 
100 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
101 {
102 	struct syscall *sc = bsearch(name, tbl->syscalls.entries,
103 				     tbl->syscalls.nr_entries, sizeof(*sc),
104 				     syscallcmpname);
105 
106 	return sc ? sc->id : -1;
107 }
108 
109 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
110 {
111 	int i;
112 	struct syscall *syscalls = tbl->syscalls.entries;
113 
114 	for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) {
115 		if (strglobmatch(syscalls[i].name, syscall_glob)) {
116 			*idx = i;
117 			return syscalls[i].id;
118 		}
119 	}
120 
121 	return -1;
122 }
123 
124 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
125 {
126 	*idx = -1;
127 	return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
128 }
129 
130 #else /* HAVE_SYSCALL_TABLE */
131 
132 #include <libaudit.h>
133 
134 struct syscalltbl *syscalltbl__new(void)
135 {
136 	struct syscalltbl *tbl = malloc(sizeof(*tbl));
137 	if (tbl)
138 		tbl->audit_machine = audit_detect_machine();
139 	return tbl;
140 }
141 
142 void syscalltbl__delete(struct syscalltbl *tbl)
143 {
144 	free(tbl);
145 }
146 
147 const char *syscalltbl__name(const struct syscalltbl *tbl, int id)
148 {
149 	return audit_syscall_to_name(id, tbl->audit_machine);
150 }
151 
152 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
153 {
154 	return audit_name_to_syscall(name, tbl->audit_machine);
155 }
156 
157 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused,
158 				  const char *syscall_glob __maybe_unused, int *idx __maybe_unused)
159 {
160 	return -1;
161 }
162 
163 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
164 {
165 	return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
166 }
167 #endif /* HAVE_SYSCALL_TABLE */
168