2 * Copyright (C) 2014 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/common.h>
12 #include <common/kernel-ctl/kernel-ctl.h>
14 #include "lttng-sessiond.h"
16 #include "lttng-syscall.h"
19 /* Global syscall table. */
20 struct syscall
*syscall_table
;
22 /* Number of entry in the syscall table. */
23 static size_t syscall_table_nb_entry
;
26 * Populate the system call table using the kernel tracer.
28 * Return 0 on success and the syscall table is allocated. On error, a negative
31 int syscall_init_table(int tracer_fd
)
36 /* Syscall data from the kernel. */
38 bool at_least_one_syscall
= false;
40 char name
[SYSCALL_NAME_LEN
];
42 DBG3("Syscall init system call table");
44 fd
= kernctl_syscall_list(tracer_fd
);
47 PERROR("kernelctl syscall list");
54 PERROR("syscall list fdopen");
58 nbmem
= SYSCALL_TABLE_INIT_SIZE
;
59 syscall_table
= zmalloc(sizeof(struct syscall
) * nbmem
);
62 PERROR("syscall list zmalloc");
67 "syscall { index = %zu; \
68 name = %" XSTR(SYSCALL_NAME_LEN
) "[^;]; \
70 &index
, name
, &bitness
) == 3) {
71 at_least_one_syscall
= true;
73 struct syscall
*new_list
;
76 /* Double memory size. */
77 new_nbmem
= max(index
+ 1, nbmem
<< 1);
78 if (new_nbmem
> (SIZE_MAX
/ sizeof(*new_list
))) {
79 /* Overflow, stop everything, something went really wrong. */
80 ERR("Syscall listing memory size overflow. Stopping");
87 DBG("Reallocating syscall table from %zu to %zu entries", nbmem
,
89 new_list
= realloc(syscall_table
, new_nbmem
* sizeof(*new_list
));
92 PERROR("syscall list realloc");
96 /* Zero out the new memory. */
97 memset(new_list
+ nbmem
, 0,
98 (new_nbmem
- nbmem
) * sizeof(*new_list
));
100 syscall_table
= new_list
;
102 syscall_table
[index
].index
= index
;
103 syscall_table
[index
].bitness
= bitness
;
104 if (lttng_strncpy(syscall_table
[index
].name
, name
,
105 sizeof(syscall_table
[index
].name
))) {
108 syscall_table
= NULL
;
112 DBG("Syscall name '%s' at index %" PRIu32 " of bitness %u",
113 syscall_table[index].name,
114 syscall_table[index].index,
115 syscall_table[index].bitness);
119 /* Index starts at 0. */
120 if (at_least_one_syscall
) {
121 syscall_table_nb_entry
= index
+ 1;
129 PERROR("syscall list fclose");
136 PERROR("syscall list close");
144 * Helper function for the list syscalls command that empty the temporary
145 * syscall hashtable used to track duplicate between 32 and 64 bit arch.
147 * This empty the hash table and destroys it after. After this, the pointer is
148 * unsuable. RCU read side lock MUST be acquired before calling this.
150 static void destroy_syscall_ht(struct lttng_ht
*ht
)
152 struct lttng_ht_iter iter
;
153 struct syscall
*ksyscall
;
155 DBG3("Destroying syscall hash table.");
161 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, ksyscall
, node
.node
) {
164 ret
= lttng_ht_del(ht
, &iter
);
172 * Allocate the given hashtable pointer.
174 * Return 0 on success else a negative LTTNG error value.
176 static int init_syscall_ht(struct lttng_ht
**ht
)
180 *ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
182 ret
= -LTTNG_ERR_NOMEM
;
191 * Lookup a syscall in the given hash table by name.
193 * Return syscall object if found or else NULL.
195 static struct syscall
*lookup_syscall(struct lttng_ht
*ht
, const char *name
)
197 struct lttng_ht_node_str
*node
;
198 struct lttng_ht_iter iter
;
199 struct syscall
*ksyscall
= NULL
;
204 lttng_ht_lookup(ht
, (void *) name
, &iter
);
205 node
= lttng_ht_iter_get_node_str(&iter
);
207 ksyscall
= caa_container_of(node
, struct syscall
, node
);
214 * Using the given syscall object in the events array with the bitness of the
215 * syscall at index in the syscall table.
217 static void update_event_syscall_bitness(struct lttng_event
*events
,
218 unsigned int index
, unsigned int syscall_index
)
220 LTTNG_ASSERT(events
);
222 if (syscall_table
[index
].bitness
== 32) {
223 events
[syscall_index
].flags
|= LTTNG_EVENT_FLAG_SYSCALL_32
;
225 events
[syscall_index
].flags
|= LTTNG_EVENT_FLAG_SYSCALL_64
;
230 * Allocate and initialize syscall object and add it to the given hashtable.
232 * Return 0 on success else -LTTNG_ERR_NOMEM.
234 static int add_syscall_to_ht(struct lttng_ht
*ht
, unsigned int index
,
235 unsigned int syscall_index
)
238 struct syscall
*ksyscall
;
242 ksyscall
= zmalloc(sizeof(*ksyscall
));
244 ret
= -LTTNG_ERR_NOMEM
;
248 strncpy(ksyscall
->name
, syscall_table
[index
].name
,
249 sizeof(ksyscall
->name
));
250 ksyscall
->bitness
= syscall_table
[index
].bitness
;
251 ksyscall
->index
= syscall_index
;
252 lttng_ht_node_init_str(&ksyscall
->node
, ksyscall
->name
);
253 lttng_ht_add_unique_str(ht
, &ksyscall
->node
);
261 * List syscalls present in the kernel syscall global array, allocate and
262 * populate the events structure with them. Skip the empty syscall name.
264 * Return the number of entries in the array else a negative value.
266 ssize_t
syscall_table_list(struct lttng_event
**_events
)
270 struct lttng_event
*events
;
271 /* Hash table used to filter duplicate out. */
272 struct lttng_ht
*syscalls_ht
= NULL
;
274 LTTNG_ASSERT(_events
);
276 DBG("Syscall table listing.");
281 * Allocate at least the number of total syscall we have even if some of
282 * them might not be valid. The count below will make sure to return the
283 * right size of the events array.
285 events
= zmalloc(syscall_table_nb_entry
* sizeof(*events
));
287 PERROR("syscall table list zmalloc");
288 ret
= -LTTNG_ERR_NOMEM
;
292 ret
= init_syscall_ht(&syscalls_ht
);
297 for (i
= 0; i
< syscall_table_nb_entry
; i
++) {
298 struct syscall
*ksyscall
;
300 /* Skip empty syscalls. */
301 if (*syscall_table
[i
].name
== '\0') {
305 ksyscall
= lookup_syscall(syscalls_ht
, syscall_table
[i
].name
);
307 update_event_syscall_bitness(events
, i
, ksyscall
->index
);
311 ret
= add_syscall_to_ht(syscalls_ht
, i
, index
);
316 /* Copy the event information in the event's array. */
317 strncpy(events
[index
].name
, syscall_table
[i
].name
,
318 sizeof(events
[index
].name
));
319 update_event_syscall_bitness(events
, i
, index
);
320 events
[index
].type
= LTTNG_EVENT_SYSCALL
;
321 /* This makes the command line not print the enabled/disabled field. */
322 events
[index
].enabled
= -1;
326 destroy_syscall_ht(syscalls_ht
);
332 destroy_syscall_ht(syscalls_ht
);