Linux Perf
syscalltbl.c
Go to the documentation of this file.
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_SUPPORT
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 #elif defined(__powerpc64__)
34 #include <asm/syscalls_64.c>
35 const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_64_MAX_ID;
36 static const char **syscalltbl_native = syscalltbl_powerpc_64;
37 #elif defined(__powerpc__)
38 #include <asm/syscalls_32.c>
39 const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_32_MAX_ID;
40 static const char **syscalltbl_native = syscalltbl_powerpc_32;
41 #endif
42 
43 struct syscall {
44  int id;
45  const char *name;
46 };
47 
48 static int syscallcmpname(const void *vkey, const void *ventry)
49 {
50  const char *key = vkey;
51  const struct syscall *entry = ventry;
52 
53  return strcmp(key, entry->name);
54 }
55 
56 static int syscallcmp(const void *va, const void *vb)
57 {
58  const struct syscall *a = va, *b = vb;
59 
60  return strcmp(a->name, b->name);
61 }
62 
63 static int syscalltbl__init_native(struct syscalltbl *tbl)
64 {
65  int nr_entries = 0, i, j;
66  struct syscall *entries;
67 
68  for (i = 0; i <= syscalltbl_native_max_id; ++i)
69  if (syscalltbl_native[i])
70  ++nr_entries;
71 
72  entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries);
73  if (tbl->syscalls.entries == NULL)
74  return -1;
75 
76  for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) {
77  if (syscalltbl_native[i]) {
78  entries[j].name = syscalltbl_native[i];
79  entries[j].id = i;
80  ++j;
81  }
82  }
83 
84  qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp);
85  tbl->syscalls.nr_entries = nr_entries;
86  return 0;
87 }
88 
89 struct syscalltbl *syscalltbl__new(void)
90 {
91  struct syscalltbl *tbl = malloc(sizeof(*tbl));
92  if (tbl) {
93  if (syscalltbl__init_native(tbl)) {
94  free(tbl);
95  return NULL;
96  }
97  }
98  return tbl;
99 }
100 
101 void syscalltbl__delete(struct syscalltbl *tbl)
102 {
103  zfree(&tbl->syscalls.entries);
104  free(tbl);
105 }
106 
107 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id)
108 {
109  return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL;
110 }
111 
112 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
113 {
114  struct syscall *sc = bsearch(name, tbl->syscalls.entries,
115  tbl->syscalls.nr_entries, sizeof(*sc),
116  syscallcmpname);
117 
118  return sc ? sc->id : -1;
119 }
120 
121 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
122 {
123  int i;
124  struct syscall *syscalls = tbl->syscalls.entries;
125 
126  for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) {
127  if (strglobmatch(syscalls[i].name, syscall_glob)) {
128  *idx = i;
129  return syscalls[i].id;
130  }
131  }
132 
133  return -1;
134 }
135 
136 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
137 {
138  *idx = -1;
139  return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
140 }
141 
142 #else /* HAVE_SYSCALL_TABLE_SUPPORT */
143 
144 #include <libaudit.h>
145 
147 {
148  struct syscalltbl *tbl = malloc(sizeof(*tbl));
149  if (tbl)
150  tbl->audit_machine = audit_detect_machine();
151  return tbl;
152 }
153 
155 {
156  free(tbl);
157 }
158 
159 const char *syscalltbl__name(const struct syscalltbl *tbl, int id)
160 {
161  return audit_syscall_to_name(id, tbl->audit_machine);
162 }
163 
164 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
165 {
166  return audit_name_to_syscall(name, tbl->audit_machine);
167 }
168 
169 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused,
170  const char *syscall_glob __maybe_unused, int *idx __maybe_unused)
171 {
172  return -1;
173 }
174 
175 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
176 {
177  return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
178 }
179 #endif /* HAVE_SYSCALL_TABLE_SUPPORT */
struct syscalltbl::@150::@152 syscalls
int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
Definition: syscalltbl.c:175
void syscalltbl__delete(struct syscalltbl *tbl)
Definition: syscalltbl.c:154
static const char * syscalltbl_x86_64[]
Definition: syscalls_64.c:1
bool strglobmatch(const char *str, const char *pat)
Definition: string.c:265
void * malloc(YYSIZE_T)
const char * key
Definition: bpf-loader.c:196
int audit_machine
Definition: syscalltbl.h:7
static int entry(u64 ip, struct unwind_info *ui)
Definition: unwind-libdw.c:71
int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused, const char *syscall_glob __maybe_unused, int *idx __maybe_unused)
Definition: syscalltbl.c:169
const char * syscalltbl__name(const struct syscalltbl *tbl, int id)
Definition: syscalltbl.c:159
int syscalltbl__id(struct syscalltbl *tbl, const char *name)
Definition: syscalltbl.c:164
#define zfree(ptr)
Definition: util.h:25
struct syscalltbl * syscalltbl__new(void)
Definition: syscalltbl.c:146
void free(void *)
#define SYSCALLTBL_x86_64_MAX_ID
Definition: syscalls_64.c:336
const char * name