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.
19 #include <common/bitfield.h>
20 #include <common/common.h>
21 #include <common/kernel-ctl/kernel-ctl.h>
23 #include "lttng-sessiond.h"
28 /* Global syscall table. */
29 struct syscall
*syscall_table
;
31 /* Number of entry in the syscall table. */
32 static size_t syscall_table_nb_entry
;
35 * Populate the system call table using the kernel tracer.
37 * Return 0 on success and the syscall table is allocated. On error, a negative
40 int syscall_init_table(void)
45 /* Syscall data from the kernel. */
48 char name
[SYSCALL_NAME_LEN
];
50 DBG3("Syscall init system call table");
52 fd
= kernctl_syscall_list(kernel_tracer_fd
);
55 PERROR("kernelctl syscall list");
62 PERROR("syscall list fdopen");
66 nbmem
= SYSCALL_TABLE_INIT_SIZE
;
67 syscall_table
= zmalloc(sizeof(struct syscall
) * nbmem
);
70 PERROR("syscall list zmalloc");
75 "syscall { index = %zu; \
76 name = %" XSTR(SYSCALL_NAME_LEN
) "[^;]; \
78 &index
, name
, &bitness
) == 3) {
79 if (index
>= nbmem
) {
80 struct syscall
*new_list
;
83 /* Double memory size. */
84 new_nbmem
= max(index
, nbmem
<< 1);
85 if (new_nbmem
< nbmem
) {
86 /* Overflow, stop everything, something went really wrong. */
87 ERR("Syscall listing memory size overflow. Stopping");
94 DBG("Reallocating syscall table from %zu to %zu entries", nbmem
,
96 new_list
= realloc(syscall_table
, new_nbmem
* sizeof(*new_list
));
99 PERROR("syscall list realloc");
103 /* Zero out the new memory. */
104 memset(new_list
+ nbmem
, 0,
105 (new_nbmem
- nbmem
) * sizeof(*new_list
));
107 syscall_table
= new_list
;
109 syscall_table
[index
].index
= index
;
110 syscall_table
[index
].bitness
= bitness
;
111 strncpy(syscall_table
[index
].name
, name
,
112 sizeof(syscall_table
[index
].name
));
114 DBG("Syscall name '%s' at index %" PRIu32 " of bitness %u",
115 syscall_table[index].name,
116 syscall_table[index].index,
117 syscall_table[index].bitness);
121 syscall_table_nb_entry
= index
;
128 PERROR("syscall list fclose");
135 PERROR("syscall list close");
143 * Helper function for the list syscalls command that empty the temporary
144 * syscall hashtable used to track duplicate between 32 and 64 bit arch.
146 * This empty the hash table and destroys it after. After this, the pointer is
147 * unsuable. RCU read side lock MUST be acquired before calling this.
149 static void destroy_syscall_ht(struct lttng_ht
*ht
)
151 struct lttng_ht_iter iter
;
152 struct syscall
*ksyscall
;
154 DBG3("Destroying syscall hash table.");
160 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, ksyscall
, node
.node
) {
163 ret
= lttng_ht_del(ht
, &iter
);
171 * Allocate the given hashtable pointer.
173 * Return 0 on success else a negative LTTNG error value.
175 static int init_syscall_ht(struct lttng_ht
**ht
)
179 *ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
181 ret
= -LTTNG_ERR_NOMEM
;
190 * Lookup a syscall in the given hash table by name.
192 * Return syscall object if found or else NULL.
194 static struct syscall
*lookup_syscall(struct lttng_ht
*ht
, const char *name
)
196 struct lttng_ht_node_str
*node
;
197 struct lttng_ht_iter iter
;
198 struct syscall
*ksyscall
= NULL
;
203 lttng_ht_lookup(ht
, (void *) name
, &iter
);
204 node
= lttng_ht_iter_get_node_str(&iter
);
206 ksyscall
= caa_container_of(node
, struct syscall
, node
);
213 * Using the given syscall object in the events array with the bitness of the
214 * syscall at index in the syscall table.
216 static void update_event_syscall_bitness(struct lttng_event
*events
,
217 unsigned int index
, unsigned int syscall_index
)
221 if (syscall_table
[index
].bitness
== 32) {
222 events
[syscall_index
].flags
|= LTTNG_EVENT_FLAG_SYSCALL_32
;
224 events
[syscall_index
].flags
|= LTTNG_EVENT_FLAG_SYSCALL_64
;
229 * Allocate and initialize syscall object and add it to the given hashtable.
231 * Return 0 on success else -LTTNG_ERR_NOMEM.
233 static int add_syscall_to_ht(struct lttng_ht
*ht
, unsigned int index
,
234 unsigned int syscall_index
)
237 struct syscall
*ksyscall
;
241 ksyscall
= zmalloc(sizeof(*ksyscall
));
243 ret
= -LTTNG_ERR_NOMEM
;
247 strncpy(ksyscall
->name
, syscall_table
[index
].name
,
248 sizeof(ksyscall
->name
));
249 ksyscall
->bitness
= syscall_table
[index
].bitness
;
250 ksyscall
->index
= syscall_index
;
251 lttng_ht_node_init_str(&ksyscall
->node
, ksyscall
->name
);
252 lttng_ht_add_unique_str(ht
, &ksyscall
->node
);
260 * List syscalls present in the kernel syscall global array, allocate and
261 * populate the events structure with them. Skip the empty syscall name.
263 * Return the number of entries in the array else a negative value.
265 ssize_t
syscall_table_list(struct lttng_event
**_events
)
269 struct lttng_event
*events
;
270 /* Hash table used to filter duplicate out. */
271 struct lttng_ht
*syscalls_ht
= NULL
;
275 DBG("Syscall table listing.");
280 * Allocate at least the number of total syscall we have even if some of
281 * them might not be valid. The count below will make sure to return the
282 * right size of the events array.
284 events
= zmalloc(syscall_table_nb_entry
* sizeof(*events
));
286 PERROR("syscall table list zmalloc");
287 ret
= -LTTNG_ERR_NOMEM
;
291 ret
= init_syscall_ht(&syscalls_ht
);
296 for (i
= 0; i
< syscall_table_nb_entry
; i
++) {
297 struct syscall
*ksyscall
;
299 /* Skip empty syscalls. */
300 if (*syscall_table
[i
].name
== '\0') {
304 ksyscall
= lookup_syscall(syscalls_ht
, syscall_table
[i
].name
);
306 update_event_syscall_bitness(events
, i
, ksyscall
->index
);
310 ret
= add_syscall_to_ht(syscalls_ht
, i
, index
);
315 /* Copy the event information in the event's array. */
316 strncpy(events
[index
].name
, syscall_table
[i
].name
,
317 sizeof(events
[index
].name
));
318 update_event_syscall_bitness(events
, i
, index
);
319 events
[index
].type
= LTTNG_EVENT_SYSCALL
;
320 /* This makes the command line not print the enabled/disabled field. */
321 events
[index
].enabled
= -1;
325 destroy_syscall_ht(syscalls_ht
);
331 destroy_syscall_ht(syscalls_ht
);
338 * Add enabled syscall to the events list using the given kernel channel.
340 * Return the number of entry of the events array that is different from size
341 * if the array grows. On error, return negative value and events is untouched.
343 ssize_t
syscall_list_channel(struct ltt_kernel_channel
*kchan
,
344 struct lttng_event
**_events
, size_t size
)
351 struct lttng_event
*events
= NULL
;
352 /* Hash table used to filter duplicate out. */
353 struct lttng_ht
*syscalls_ht
= NULL
;
357 /* Get syscall mask from the kernel tracer. */
358 err
= kernel_syscall_mask(kchan
->fd
, &mask
, &len
);
364 ret
= init_syscall_ht(&syscalls_ht
);
369 count
= new_size
= size
;
372 for (i
= 0; i
< len
; i
++) {
374 struct syscall
*ksyscall
;
376 bitfield_read_be(mask
, unsigned char, i
, 1, &val
);
378 /* Syscall is disabled, continue the loop. */
382 /* Skip empty syscall. */
383 if (*syscall_table
[i
].name
== '\0') {
387 /* Syscall is enabled thus add it to the events list. */
389 if (count
>= new_size
) {
390 struct lttng_event
*new_events
;
392 /* Get the maximum here since count can be 0. */
393 new_size
= max(count
<< 1, 1);
394 DBG3("Listing syscall realloc events array from %zu to %zu", count
,
396 new_events
= realloc(events
, new_size
* sizeof(*new_events
));
398 PERROR("realloc kernel events list");
402 memset(new_events
+ count
, 0,
403 (new_size
- count
) * sizeof(*new_events
));
408 ksyscall
= lookup_syscall(syscalls_ht
, syscall_table
[i
].name
);
410 update_event_syscall_bitness(events
, i
, ksyscall
->index
);
417 ret
= add_syscall_to_ht(syscalls_ht
, i
, count
);
422 update_event_syscall_bitness(events
, i
, count
);
423 strncpy(events
[count
].name
, syscall_table
[i
].name
,
424 sizeof(events
[count
].name
));
425 events
[count
].enabled
= 1;
426 events
[count
].type
= LTTNG_EVENT_SYSCALL
;
436 destroy_syscall_ht(syscalls_ht
);