Skip to content

Commit 5af56fa

Browse files
committed
perf tools: Allow generating per-arch syscall table arrays
Tools should use a mechanism similar to arch/x86/entry/syscalls/ to generate a header file with the definitions for two variables: static const char *syscalltbl_x86_64[] = { [0] = "read", [1] = "write", <SNIP> [324] = "membarrier", [325] = "mlock2", [326] = "copy_file_range", }; static const int syscalltbl_x86_64_max_id = 326; In a per arch file that should then be included in tools/perf/util/syscalltbl.c. First one will be for x86_64. Cc: Adrian Hunter <[email protected]> Cc: David Ahern <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Wang Nan <[email protected]> Link: http://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent fd0db10 commit 5af56fa

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

tools/perf/util/syscalltbl.c

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,103 @@
1414
*/
1515

1616
#include "syscalltbl.h"
17+
#include <stdlib.h>
18+
19+
#ifdef HAVE_SYSCALL_TABLE
20+
#include <linux/compiler.h>
1721
#include <string.h>
18-
#include <libaudit.h>
22+
#include "util.h"
23+
24+
struct syscall {
25+
int id;
26+
const char *name;
27+
};
1928

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+
}
2069

2170
struct syscalltbl *syscalltbl__new(void)
2271
{
2372
struct syscalltbl *tbl = malloc(sizeof(*tbl));
2473
if (tbl) {
25-
tbl->audit_machine = audit_detect_machine();
74+
if (syscalltbl__init_native(tbl)) {
75+
free(tbl);
76+
return NULL;
77+
}
2678
}
2779
return tbl;
2880
}
2981

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+
30114
void syscalltbl__delete(struct syscalltbl *tbl)
31115
{
32116
free(tbl);
@@ -41,3 +125,4 @@ int syscalltbl__id(struct syscalltbl *tbl, const char *name)
41125
{
42126
return audit_name_to_syscall(name, tbl->audit_machine);
43127
}
128+
#endif /* HAVE_SYSCALL_TABLE */

tools/perf/util/syscalltbl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
struct syscalltbl {
55
union {
66
int audit_machine;
7+
struct {
8+
int nr_entries;
9+
void *entries;
10+
} syscalls;
711
};
812
};
913

0 commit comments

Comments
 (0)