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/bitfield.h>
22 #include <common/common.h>
23 #include <common/kernel-ctl/kernel-ctl.h>
25 #include "lttng-sessiond.h"
30 /* Global syscall table. */
31 struct syscall
*syscall_table
;
33 /* Number of entry in the syscall table. */
34 static size_t syscall_table_nb_entry
;
37 * Populate the system call table using the kernel tracer.
39 * Return 0 on success and the syscall table is allocated. On error, a negative
42 int syscall_init_table(void)
47 /* Syscall data from the kernel. */
49 bool at_least_one_syscall
= false;
51 char name
[SYSCALL_NAME_LEN
];
53 DBG3("Syscall init system call table");
55 fd
= kernctl_syscall_list(kernel_tracer_fd
);
58 PERROR("kernelctl syscall list");
65 PERROR("syscall list fdopen");
69 nbmem
= SYSCALL_TABLE_INIT_SIZE
;
70 syscall_table
= zmalloc(sizeof(struct syscall
) * nbmem
);
73 PERROR("syscall list zmalloc");
78 "syscall { index = %zu; \
79 name = %" XSTR(SYSCALL_NAME_LEN
) "[^;]; \
81 &index
, name
, &bitness
) == 3) {
82 at_least_one_syscall
= true;
84 struct syscall
*new_list
;
87 /* Double memory size. */
88 new_nbmem
= max(index
+ 1, nbmem
<< 1);
89 if (new_nbmem
> (SIZE_MAX
/ sizeof(*new_list
))) {
90 /* Overflow, stop everything, something went really wrong. */
91 ERR("Syscall listing memory size overflow. Stopping");
98 DBG("Reallocating syscall table from %zu to %zu entries", nbmem
,
100 new_list
= realloc(syscall_table
, new_nbmem
* sizeof(*new_list
));
103 PERROR("syscall list realloc");
107 /* Zero out the new memory. */
108 memset(new_list
+ nbmem
, 0,
109 (new_nbmem
- nbmem
) * sizeof(*new_list
));
111 syscall_table
= new_list
;
113 syscall_table
[index
].index
= index
;
114 syscall_table
[index
].bitness
= bitness
;
115 if (lttng_strncpy(syscall_table
[index
].name
, name
,
116 sizeof(syscall_table
[index
].name
))) {
119 syscall_table
= NULL
;
123 DBG("Syscall name '%s' at index %" PRIu32 " of bitness %u",
124 syscall_table[index].name,
125 syscall_table[index].index,
126 syscall_table[index].bitness);
130 /* Index starts at 0. */
131 if (at_least_one_syscall
) {
132 syscall_table_nb_entry
= index
+ 1;
140 PERROR("syscall list fclose");
147 PERROR("syscall list close");
155 * Helper function for the list syscalls command that empty the temporary
156 * syscall hashtable used to track duplicate between 32 and 64 bit arch.
158 * This empty the hash table and destroys it after. After this, the pointer is
159 * unsuable. RCU read side lock MUST be acquired before calling this.
161 static void destroy_syscall_ht(struct lttng_ht
*ht
)
163 struct lttng_ht_iter iter
;
164 struct syscall
*ksyscall
;
166 DBG3("Destroying syscall hash table.");
172 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, ksyscall
, node
.node
) {
175 ret
= lttng_ht_del(ht
, &iter
);
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 * Return syscall object if found or else NULL.
206 static struct syscall
*lookup_syscall(struct lttng_ht
*ht
, const char *name
)
208 struct lttng_ht_node_str
*node
;
209 struct lttng_ht_iter iter
;
210 struct syscall
*ksyscall
= NULL
;
215 lttng_ht_lookup(ht
, (void *) name
, &iter
);
216 node
= lttng_ht_iter_get_node_str(&iter
);
218 ksyscall
= caa_container_of(node
, struct syscall
, node
);
225 * Using the given syscall object in the events array with the bitness of the
226 * syscall at index in the syscall table.
228 static void update_event_syscall_bitness(struct lttng_event
*events
,
229 unsigned int index
, unsigned int syscall_index
)
233 if (syscall_table
[index
].bitness
== 32) {
234 events
[syscall_index
].flags
|= LTTNG_EVENT_FLAG_SYSCALL_32
;
236 events
[syscall_index
].flags
|= LTTNG_EVENT_FLAG_SYSCALL_64
;
241 * Allocate and initialize syscall object and add it to the given hashtable.
243 * Return 0 on success else -LTTNG_ERR_NOMEM.
245 static int add_syscall_to_ht(struct lttng_ht
*ht
, unsigned int index
,
246 unsigned int syscall_index
)
249 struct syscall
*ksyscall
;
253 ksyscall
= zmalloc(sizeof(*ksyscall
));
255 ret
= -LTTNG_ERR_NOMEM
;
259 strncpy(ksyscall
->name
, syscall_table
[index
].name
,
260 sizeof(ksyscall
->name
));
261 ksyscall
->bitness
= syscall_table
[index
].bitness
;
262 ksyscall
->index
= syscall_index
;
263 lttng_ht_node_init_str(&ksyscall
->node
, ksyscall
->name
);
264 lttng_ht_add_unique_str(ht
, &ksyscall
->node
);
272 * List syscalls present in the kernel syscall global array, allocate and
273 * populate the events structure with them. Skip the empty syscall name.
275 * Return the number of entries in the array else a negative value.
277 ssize_t
syscall_table_list(struct lttng_event
**_events
)
281 struct lttng_event
*events
;
282 /* Hash table used to filter duplicate out. */
283 struct lttng_ht
*syscalls_ht
= NULL
;
287 DBG("Syscall table listing.");
292 * Allocate at least the number of total syscall we have even if some of
293 * them might not be valid. The count below will make sure to return the
294 * right size of the events array.
296 events
= zmalloc(syscall_table_nb_entry
* sizeof(*events
));
298 PERROR("syscall table list zmalloc");
299 ret
= -LTTNG_ERR_NOMEM
;
303 ret
= init_syscall_ht(&syscalls_ht
);
308 for (i
= 0; i
< syscall_table_nb_entry
; i
++) {
309 struct syscall
*ksyscall
;
311 /* Skip empty syscalls. */
312 if (*syscall_table
[i
].name
== '\0') {
316 ksyscall
= lookup_syscall(syscalls_ht
, syscall_table
[i
].name
);
318 update_event_syscall_bitness(events
, i
, ksyscall
->index
);
322 ret
= add_syscall_to_ht(syscalls_ht
, i
, index
);
327 /* Copy the event information in the event's array. */
328 strncpy(events
[index
].name
, syscall_table
[i
].name
,
329 sizeof(events
[index
].name
));
330 update_event_syscall_bitness(events
, i
, index
);
331 events
[index
].type
= LTTNG_EVENT_SYSCALL
;
332 /* This makes the command line not print the enabled/disabled field. */
333 events
[index
].enabled
= -1;
337 destroy_syscall_ht(syscalls_ht
);
343 destroy_syscall_ht(syscalls_ht
);