eb74a97b1f11a187afb15c0a24b870679e00b47c
[deliverable/linux.git] / tools / perf / util / syscalltbl.c
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
19 #ifdef HAVE_SYSCALL_TABLE
20 #include <linux/compiler.h>
21 #include <string.h>
22 #include "util.h"
23
24 struct syscall {
25 int id;
26 const char *name;
27 };
28
29 static int syscallcmpname(const void *vkey, const void *ventry)
30 {
31 const char *key = vkey;
32 const struct syscall *entry = ventry;
33
34 return strcmp(key, entry->name);
35 }
36
37 static int syscallcmp(const void *va, const void *vb)
38 {
39 const struct syscall *a = va, *b = vb;
40
41 return strcmp(a->name, b->name);
42 }
43
44 static int syscalltbl__init_native(struct syscalltbl *tbl)
45 {
46 int nr_entries = 0, i, j;
47 struct syscall *entries;
48
49 for (i = 0; i <= syscalltbl_native_max_id; ++i)
50 if (syscalltbl_native[i])
51 ++nr_entries;
52
53 entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries);
54 if (tbl->syscalls.entries == NULL)
55 return -1;
56
57 for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) {
58 if (syscalltbl_native[i]) {
59 entries[j].name = syscalltbl_native[i];
60 entries[j].id = i;
61 ++j;
62 }
63 }
64
65 qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp);
66 tbl->syscalls.nr_entries = nr_entries;
67 return 0;
68 }
69
70 struct syscalltbl *syscalltbl__new(void)
71 {
72 struct syscalltbl *tbl = malloc(sizeof(*tbl));
73 if (tbl) {
74 if (syscalltbl__init_native(tbl)) {
75 free(tbl);
76 return NULL;
77 }
78 }
79 return tbl;
80 }
81
82 void syscalltbl__delete(struct syscalltbl *tbl)
83 {
84 zfree(&tbl->syscalls.entries);
85 free(tbl);
86 }
87
88 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id)
89 {
90 return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL;
91 }
92
93 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
94 {
95 struct syscall *sc = bsearch(name, tbl->syscalls.entries,
96 tbl->syscalls.nr_entries, sizeof(*sc),
97 syscallcmpname);
98
99 return sc ? sc->id : -1;
100 }
101
102 #else /* HAVE_SYSCALL_TABLE */
103
104 #include <libaudit.h>
105
106 struct syscalltbl *syscalltbl__new(void)
107 {
108 struct syscalltbl *tbl = malloc(sizeof(*tbl));
109 if (tbl)
110 tbl->audit_machine = audit_detect_machine();
111 return tbl;
112 }
113
114 void syscalltbl__delete(struct syscalltbl *tbl)
115 {
116 free(tbl);
117 }
118
119 const char *syscalltbl__name(const struct syscalltbl *tbl, int id)
120 {
121 return audit_syscall_to_name(id, tbl->audit_machine);
122 }
123
124 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
125 {
126 return audit_name_to_syscall(name, tbl->audit_machine);
127 }
128 #endif /* HAVE_SYSCALL_TABLE */
This page took 0.041481 seconds and 4 git commands to generate.