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.
20 #include <common/bitfield.h>
21 #include <common/common.h>
22 #include <common/kernel-ctl/kernel-ctl.h>
24 #include "lttng-sessiond.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. */
49 char name
[SYSCALL_NAME_LEN
];
51 DBG3("Syscall init system call table");
53 fd
= kernctl_syscall_list(kernel_tracer_fd
);
56 PERROR("kernelctl syscall list");
63 PERROR("syscall list fdopen");
67 nbmem
= SYSCALL_TABLE_INIT_SIZE
;
68 syscall_table
= zmalloc(sizeof(struct syscall
) * nbmem
);
71 PERROR("syscall list zmalloc");
76 "syscall { index = %lu; \
77 name = %" XSTR(SYSCALL_NAME_LEN
) "[^;]; \
79 &index
, name
, &bitness
) == 3) {
80 if (index
>= nbmem
) {
81 struct syscall
*new_list
;
84 /* Double memory size. */
85 new_nbmem
= max(index
, nbmem
<< 1);
86 if (new_nbmem
< nbmem
) {
87 /* Overflow, stop everything, something went really wrong. */
88 ERR("Syscall listing memory size overflow. Stopping");
95 DBG("Reallocating syscall table from %zu to %zu entries", nbmem
,
97 new_list
= realloc(syscall_table
, new_nbmem
* sizeof(*new_list
));
100 PERROR("syscall list realloc");
104 /* Zero out the new memory. */
105 memset(new_list
+ nbmem
, 0,
106 (new_nbmem
- nbmem
) * sizeof(*new_list
));
108 syscall_table
= new_list
;
110 syscall_table
[index
].index
= index
;
111 syscall_table
[index
].bitness
= bitness
;
112 strncpy(syscall_table
[index
].name
, name
,
113 sizeof(syscall_table
[index
].name
));
115 DBG("Syscall name '%s' at index %" PRIu32 " of bitness %u",
116 syscall_table[index].name,
117 syscall_table[index].index,
118 syscall_table[index].bitness);
122 syscall_table_nb_entry
= index
;
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
)
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
;
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
);
339 * Add enabled syscall to the events list using the given kernel channel.
341 * Return the number of entry of the events array that is different from size
342 * if the array grows. On error, return negative value and events is untouched.
344 ssize_t
syscall_list_channel(struct ltt_kernel_channel
*kchan
,
345 struct lttng_event
**_events
, size_t size
)
352 struct lttng_event
*events
= NULL
;
353 /* Hash table used to filter duplicate out. */
354 struct lttng_ht
*syscalls_ht
= NULL
;
358 /* Get syscall mask from the kernel tracer. */
359 err
= kernel_syscall_mask(kchan
->fd
, &mask
, &len
);
365 ret
= init_syscall_ht(&syscalls_ht
);
370 count
= new_size
= size
;
373 for (i
= 0; i
< len
; i
++) {
375 struct syscall
*ksyscall
;
377 bitfield_read_be(mask
, unsigned char, i
, 1, &val
);
379 /* Syscall is disabled, continue the loop. */
383 /* Skip empty syscall. */
384 if (*syscall_table
[i
].name
== '\0') {
388 /* Syscall is enabled thus add it to the events list. */
390 if (count
>= new_size
) {
391 struct lttng_event
*new_events
;
393 /* Get the maximum here since count can be 0. */
394 new_size
= max(count
<< 1, 1);
395 DBG3("Listing syscall realloc events array from %zu to %zu", count
,
397 new_events
= realloc(events
, new_size
* sizeof(*new_events
));
399 PERROR("realloc kernel events list");
403 memset(new_events
+ count
, 0,
404 (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
);
414 ret
= add_syscall_to_ht(syscalls_ht
, i
, count
);
419 update_event_syscall_bitness(events
, i
, count
);
420 strncpy(events
[count
].name
, syscall_table
[i
].name
,
421 sizeof(events
[count
].name
));
422 events
[count
].enabled
= 1;
423 events
[count
].type
= LTTNG_KERNEL_SYSCALL
;
432 destroy_syscall_ht(syscalls_ht
);