xref: /openbmc/linux/tools/perf/util/syscalltbl.c (revision 60772e48)
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 #elif defined(__s390x__)
30 #include <asm/syscalls_64.c>
31 const int syscalltbl_native_max_id = SYSCALLTBL_S390_64_MAX_ID;
32 static const char **syscalltbl_native = syscalltbl_s390_64;
33 #endif
34 
35 struct syscall {
36 	int id;
37 	const char *name;
38 };
39 
40 static int syscallcmpname(const void *vkey, const void *ventry)
41 {
42 	const char *key = vkey;
43 	const struct syscall *entry = ventry;
44 
45 	return strcmp(key, entry->name);
46 }
47 
48 static int syscallcmp(const void *va, const void *vb)
49 {
50 	const struct syscall *a = va, *b = vb;
51 
52 	return strcmp(a->name, b->name);
53 }
54 
55 static int syscalltbl__init_native(struct syscalltbl *tbl)
56 {
57 	int nr_entries = 0, i, j;
58 	struct syscall *entries;
59 
60 	for (i = 0; i <= syscalltbl_native_max_id; ++i)
61 		if (syscalltbl_native[i])
62 			++nr_entries;
63 
64 	entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries);
65 	if (tbl->syscalls.entries == NULL)
66 		return -1;
67 
68 	for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) {
69 		if (syscalltbl_native[i]) {
70 			entries[j].name = syscalltbl_native[i];
71 			entries[j].id = i;
72 			++j;
73 		}
74 	}
75 
76 	qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp);
77 	tbl->syscalls.nr_entries = nr_entries;
78 	return 0;
79 }
80 
81 struct syscalltbl *syscalltbl__new(void)
82 {
83 	struct syscalltbl *tbl = malloc(sizeof(*tbl));
84 	if (tbl) {
85 		if (syscalltbl__init_native(tbl)) {
86 			free(tbl);
87 			return NULL;
88 		}
89 	}
90 	return tbl;
91 }
92 
93 void syscalltbl__delete(struct syscalltbl *tbl)
94 {
95 	zfree(&tbl->syscalls.entries);
96 	free(tbl);
97 }
98 
99 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id)
100 {
101 	return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL;
102 }
103 
104 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
105 {
106 	struct syscall *sc = bsearch(name, tbl->syscalls.entries,
107 				     tbl->syscalls.nr_entries, sizeof(*sc),
108 				     syscallcmpname);
109 
110 	return sc ? sc->id : -1;
111 }
112 
113 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
114 {
115 	int i;
116 	struct syscall *syscalls = tbl->syscalls.entries;
117 
118 	for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) {
119 		if (strglobmatch(syscalls[i].name, syscall_glob)) {
120 			*idx = i;
121 			return syscalls[i].id;
122 		}
123 	}
124 
125 	return -1;
126 }
127 
128 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
129 {
130 	*idx = -1;
131 	return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
132 }
133 
134 #else /* HAVE_SYSCALL_TABLE */
135 
136 #include <libaudit.h>
137 
138 struct syscalltbl *syscalltbl__new(void)
139 {
140 	struct syscalltbl *tbl = malloc(sizeof(*tbl));
141 	if (tbl)
142 		tbl->audit_machine = audit_detect_machine();
143 	return tbl;
144 }
145 
146 void syscalltbl__delete(struct syscalltbl *tbl)
147 {
148 	free(tbl);
149 }
150 
151 const char *syscalltbl__name(const struct syscalltbl *tbl, int id)
152 {
153 	return audit_syscall_to_name(id, tbl->audit_machine);
154 }
155 
156 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
157 {
158 	return audit_name_to_syscall(name, tbl->audit_machine);
159 }
160 
161 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused,
162 				  const char *syscall_glob __maybe_unused, int *idx __maybe_unused)
163 {
164 	return -1;
165 }
166 
167 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
168 {
169 	return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
170 }
171 #endif /* HAVE_SYSCALL_TABLE */
172