2 * Copyright (C) 2014 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <common/common.h>
22 #include <common/kernel-ctl/kernel-ctl.h>
24 #include "lttng-sessiond.h"
26 #include "lttng-syscall.h"
29 /* Global syscall table. */
30 struct syscall
*syscall_table
;
32 /* Number of entry in the syscall table. */
33 static size_t syscall_table_nb_entry
;
36 * Populate the system call table using the kernel tracer.
38 * Return 0 on success and the syscall table is allocated. On error, a negative
41 int syscall_init_table(void)
46 /* Syscall data from the kernel. */
48 bool at_least_one_syscall
= false;
50 char name
[SYSCALL_NAME_LEN
];
52 DBG3("Syscall init system call table");
54 fd
= kernctl_syscall_list(kernel_tracer_fd
);
57 PERROR("kernelctl syscall list");
64 PERROR("syscall list fdopen");
68 nbmem
= SYSCALL_TABLE_INIT_SIZE
;
69 syscall_table
= zmalloc(sizeof(struct syscall
) * nbmem
);
72 PERROR("syscall list zmalloc");
77 "syscall { index = %zu; \
78 name = %" XSTR(SYSCALL_NAME_LEN
) "[^;]; \
80 &index
, name
, &bitness
) == 3) {
81 at_least_one_syscall
= true;
83 struct syscall
*new_list
;
86 /* Double memory size. */
87 new_nbmem
= max(index
+ 1, nbmem
<< 1);
88 if (new_nbmem
> (SIZE_MAX
/ sizeof(*new_list
))) {
89 /* Overflow, stop everything, something went really wrong. */
90 ERR("Syscall listing memory size overflow. Stopping");
97 DBG("Reallocating syscall table from %zu to %zu entries", nbmem
,
99 new_list
= realloc(syscall_table
, new_nbmem
* sizeof(*new_list
));
102 PERROR("syscall list realloc");
106 /* Zero out the new memory. */
107 memset(new_list
+ nbmem
, 0,
108 (new_nbmem
- nbmem
) * sizeof(*new_list
));
110 syscall_table
= new_list
;
112 syscall_table
[index
].index
= index
;
113 syscall_table
[index
].bitness
= bitness
;
114 if (lttng_strncpy(syscall_table
[index
].name
, name
,
115 sizeof(syscall_table
[index
].name
))) {
118 syscall_table
= NULL
;
122 DBG("Syscall name '%s' at index %" PRIu32 " of bitness %u",
123 syscall_table[index].name,
124 syscall_table[index].index,
125 syscall_table[index].bitness);
129 /* Index starts at 0. */
130 if (at_least_one_syscall
) {
131 syscall_table_nb_entry
= index
+ 1;
139 PERROR("syscall list fclose");
146 PERROR("syscall list close");
154 * Helper function for the list syscalls command that empty the temporary
155 * syscall hashtable used to track duplicate between 32 and 64 bit arch.
157 * This empty the hash table and destroys it after. After this, the pointer is
158 * unsuable. RCU read side lock MUST be acquired before calling this.
160 static void destroy_syscall_ht(struct lttng_ht
*ht
)
162 struct lttng_ht_iter iter
;
163 struct syscall
*ksyscall
;
165 DBG3("Destroying syscall hash table.");
171 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, ksyscall
, node
.node
) {
174 ret
= lttng_ht_del(ht
, &iter
);
182 * Allocate the given hashtable pointer.
184 * Return 0 on success else a negative LTTNG error value.
186 static int init_syscall_ht(struct lttng_ht
**ht
)
190 *ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
192 ret
= -LTTNG_ERR_NOMEM
;
201 * Lookup a syscall in the given hash table by name.
203 * Return syscall object if found or else NULL.
205 static struct syscall
*lookup_syscall(struct lttng_ht
*ht
, const char *name
)
207 struct lttng_ht_node_str
*node
;
208 struct lttng_ht_iter iter
;
209 struct syscall
*ksyscall
= NULL
;
214 lttng_ht_lookup(ht
, (void *) name
, &iter
);
215 node
= lttng_ht_iter_get_node_str(&iter
);
217 ksyscall
= caa_container_of(node
, struct syscall
, node
);
224 * Using the given syscall object in the events array with the bitness of the
225 * syscall at index in the syscall table.
227 static void update_event_syscall_bitness(struct lttng_event
*events
,
228 unsigned int index
, unsigned int syscall_index
)
232 if (syscall_table
[index
].bitness
== 32) {
233 events
[syscall_index
].flags
|= LTTNG_EVENT_FLAG_SYSCALL_32
;
235 events
[syscall_index
].flags
|= LTTNG_EVENT_FLAG_SYSCALL_64
;
240 * Allocate and initialize syscall object and add it to the given hashtable.
242 * Return 0 on success else -LTTNG_ERR_NOMEM.
244 static int add_syscall_to_ht(struct lttng_ht
*ht
, unsigned int index
,
245 unsigned int syscall_index
)
248 struct syscall
*ksyscall
;
252 ksyscall
= zmalloc(sizeof(*ksyscall
));
254 ret
= -LTTNG_ERR_NOMEM
;
258 strncpy(ksyscall
->name
, syscall_table
[index
].name
,
259 sizeof(ksyscall
->name
));
260 ksyscall
->bitness
= syscall_table
[index
].bitness
;
261 ksyscall
->index
= syscall_index
;
262 lttng_ht_node_init_str(&ksyscall
->node
, ksyscall
->name
);
263 lttng_ht_add_unique_str(ht
, &ksyscall
->node
);
271 * List syscalls present in the kernel syscall global array, allocate and
272 * populate the events structure with them. Skip the empty syscall name.
274 * Return the number of entries in the array else a negative value.
276 ssize_t
syscall_table_list(struct lttng_event
**_events
)
280 struct lttng_event
*events
;
281 /* Hash table used to filter duplicate out. */
282 struct lttng_ht
*syscalls_ht
= NULL
;
286 DBG("Syscall table listing.");
291 * Allocate at least the number of total syscall we have even if some of
292 * them might not be valid. The count below will make sure to return the
293 * right size of the events array.
295 events
= zmalloc(syscall_table_nb_entry
* sizeof(*events
));
297 PERROR("syscall table list zmalloc");
298 ret
= -LTTNG_ERR_NOMEM
;
302 ret
= init_syscall_ht(&syscalls_ht
);
307 for (i
= 0; i
< syscall_table_nb_entry
; i
++) {
308 struct syscall
*ksyscall
;
310 /* Skip empty syscalls. */
311 if (*syscall_table
[i
].name
== '\0') {
315 ksyscall
= lookup_syscall(syscalls_ht
, syscall_table
[i
].name
);
317 update_event_syscall_bitness(events
, i
, ksyscall
->index
);
321 ret
= add_syscall_to_ht(syscalls_ht
, i
, index
);
326 /* Copy the event information in the event's array. */
327 strncpy(events
[index
].name
, syscall_table
[i
].name
,
328 sizeof(events
[index
].name
));
329 update_event_syscall_bitness(events
, i
, index
);
330 events
[index
].type
= LTTNG_EVENT_SYSCALL
;
331 /* This makes the command line not print the enabled/disabled field. */
332 events
[index
].enabled
= -1;
336 destroy_syscall_ht(syscalls_ht
);
342 destroy_syscall_ht(syscalls_ht
);