2 * Copyright (C) 2014 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include "lttng-sessiond.hpp"
11 #include "lttng-syscall.hpp"
14 #include <common/common.hpp>
15 #include <common/kernel-ctl/kernel-ctl.hpp>
16 #include <common/urcu.hpp>
20 /* Global syscall table. */
21 struct syscall
*syscall_table
;
23 /* Number of entry in the syscall table. */
24 static size_t syscall_table_nb_entry
;
27 * Populate the system call table using the kernel tracer.
29 * Return 0 on success and the syscall table is allocated. On error, a negative
32 int syscall_init_table(int tracer_fd
)
37 /* Syscall data from the kernel. */
39 bool at_least_one_syscall
= false;
41 char name
[SYSCALL_NAME_LEN
];
43 #if (SYSCALL_NAME_LEN == 255)
44 #define SYSCALL_NAME_LEN_SCANF_IS_A_BROKEN_API "254"
47 DBG3("Syscall init system call table");
49 fd
= kernctl_syscall_list(tracer_fd
);
52 PERROR("kernelctl syscall list");
59 PERROR("syscall list fdopen");
63 nbmem
= SYSCALL_TABLE_INIT_SIZE
;
64 syscall_table
= calloc
<struct syscall
>(nbmem
);
67 PERROR("syscall list zmalloc");
72 "syscall { index = %zu; \
73 name = %" SYSCALL_NAME_LEN_SCANF_IS_A_BROKEN_API
"[^;]; \
78 at_least_one_syscall
= true;
80 struct syscall
*new_list
;
83 /* Double memory size. */
84 new_nbmem
= std::max(index
+ 1, nbmem
<< 1);
85 if (new_nbmem
> (SIZE_MAX
/ sizeof(*new_list
))) {
86 /* Overflow, stop everything, something went really wrong. */
87 ERR("Syscall listing memory size overflow. Stopping");
89 syscall_table
= nullptr;
94 DBG("Reallocating syscall table from %zu to %zu entries", nbmem
, new_nbmem
);
95 new_list
= (struct syscall
*) realloc(syscall_table
,
96 new_nbmem
* sizeof(*new_list
));
99 PERROR("syscall list realloc");
103 /* Zero out the new memory. */
104 memset(new_list
+ nbmem
, 0, (new_nbmem
- nbmem
) * sizeof(*new_list
));
106 syscall_table
= new_list
;
108 syscall_table
[index
].index
= index
;
109 syscall_table
[index
].bitness
= bitness
;
111 syscall_table
[index
].name
, name
, sizeof(syscall_table
[index
].name
))) {
114 syscall_table
= nullptr;
118 DBG("Syscall name '%s' at index %" PRIu32 " of bitness %u",
119 syscall_table[index].name,
120 syscall_table[index].index,
121 syscall_table[index].bitness);
125 /* Index starts at 0. */
126 if (at_least_one_syscall
) {
127 syscall_table_nb_entry
= index
+ 1;
135 PERROR("syscall list fclose");
142 PERROR("syscall list close");
150 * Helper function for the list syscalls command that empty the temporary
151 * syscall hashtable used to track duplicate between 32 and 64 bit arch.
153 * This empty the hash table and destroys it after. After this, the pointer is
154 * unsuable. RCU read side lock MUST NOT be acquired before calling this.
156 static void destroy_syscall_ht(struct lttng_ht
*ht
)
158 struct lttng_ht_iter iter
;
159 struct syscall
*ksyscall
;
161 DBG3("Destroying syscall hash table.");
168 lttng::urcu::read_lock_guard read_lock
;
170 cds_lfht_for_each_entry (ht
->ht
, &iter
.iter
, ksyscall
, node
.node
) {
173 ret
= lttng_ht_del(ht
, &iter
);
179 lttng_ht_destroy(ht
);
183 * Allocate the given hashtable pointer.
185 * Return 0 on success else a negative LTTNG error value.
187 static int init_syscall_ht(struct lttng_ht
**ht
)
191 *ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
193 ret
= -LTTNG_ERR_NOMEM
;
202 * Lookup a syscall in the given hash table by name.
204 * RCU read lock MUST be acquired by the callers of this function.
206 * Return syscall object if found or else NULL.
208 static struct syscall
*lookup_syscall(struct lttng_ht
*ht
, const char *name
)
210 struct lttng_ht_node_str
*node
;
211 struct lttng_ht_iter iter
;
212 struct syscall
*ksyscall
= nullptr;
217 lttng_ht_lookup(ht
, (void *) name
, &iter
);
218 node
= lttng_ht_iter_get_node_str(&iter
);
220 ksyscall
= lttng::utils::container_of(node
, &syscall::node
);
227 * Using the given syscall object in the events array with the bitness of the
228 * syscall at index in the syscall table.
230 static void update_event_syscall_bitness(struct lttng_event
*events
,
232 unsigned int syscall_index
)
234 LTTNG_ASSERT(events
);
236 if (syscall_table
[index
].bitness
== 32) {
237 events
[syscall_index
].flags
= (lttng_event_flag
) (events
[syscall_index
].flags
|
238 LTTNG_EVENT_FLAG_SYSCALL_32
);
240 events
[syscall_index
].flags
= (lttng_event_flag
) (events
[syscall_index
].flags
|
241 LTTNG_EVENT_FLAG_SYSCALL_64
);
246 * Allocate and initialize syscall object and add it to the given hashtable.
248 * Return 0 on success else -LTTNG_ERR_NOMEM.
250 static int add_syscall_to_ht(struct lttng_ht
*ht
, unsigned int index
, unsigned int syscall_index
)
253 struct syscall
*ksyscall
;
257 ksyscall
= zmalloc
<struct syscall
>();
259 ret
= -LTTNG_ERR_NOMEM
;
263 strncpy(ksyscall
->name
, syscall_table
[index
].name
, sizeof(ksyscall
->name
));
264 ksyscall
->bitness
= syscall_table
[index
].bitness
;
265 ksyscall
->index
= syscall_index
;
266 lttng_ht_node_init_str(&ksyscall
->node
, ksyscall
->name
);
267 lttng_ht_add_unique_str(ht
, &ksyscall
->node
);
275 * List syscalls present in the kernel syscall global array, allocate and
276 * populate the events structure with them. Skip the empty syscall name.
278 * Return the number of entries in the array else a negative value.
280 ssize_t
syscall_table_list(struct lttng_event
**_events
)
284 struct lttng_event
*events
;
285 /* Hash table used to filter duplicate out. */
286 struct lttng_ht
*syscalls_ht
= nullptr;
288 LTTNG_ASSERT(_events
);
290 DBG("Syscall table listing.");
293 * Allocate at least the number of total syscall we have even if some of
294 * them might not be valid. The count below will make sure to return the
295 * right size of the events array.
297 events
= calloc
<lttng_event
>(syscall_table_nb_entry
);
299 PERROR("syscall table list zmalloc");
300 ret
= -LTTNG_ERR_NOMEM
;
304 ret
= init_syscall_ht(&syscalls_ht
);
309 for (i
= 0; i
< syscall_table_nb_entry
; i
++) {
310 /* Skip empty syscalls. */
311 if (*syscall_table
[i
].name
== '\0') {
316 lttng::urcu::read_lock_guard read_lock
;
317 struct syscall
*ksyscall
;
319 ksyscall
= lookup_syscall(syscalls_ht
, syscall_table
[i
].name
);
321 update_event_syscall_bitness(events
, i
, ksyscall
->index
);
326 ret
= add_syscall_to_ht(syscalls_ht
, i
, index
);
331 /* Copy the event information in the event's array. */
332 strncpy(events
[index
].name
, syscall_table
[i
].name
, sizeof(events
[index
].name
));
333 update_event_syscall_bitness(events
, i
, index
);
334 events
[index
].type
= LTTNG_EVENT_SYSCALL
;
335 /* This makes the command line not print the enabled/disabled field. */
336 events
[index
].enabled
= -1;
340 destroy_syscall_ht(syscalls_ht
);
345 destroy_syscall_ht(syscalls_ht
);