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
38 * value is returned and the syscall table is set to NULL.
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 = %lu; \
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
= index
<< 1;
86 DBG("Reallocating syscall table from %zu to %zu entries", nbmem
,
88 new_list
= realloc(syscall_table
, new_nbmem
* sizeof(*new_list
));
91 PERROR("syscall list realloc");
95 /* Zero out the new memory. */
96 memset(new_list
+ nbmem
, 0,
97 (new_nbmem
- nbmem
) * sizeof(*new_list
));
99 syscall_table
= new_list
;
101 syscall_table
[index
].index
= index
;
102 syscall_table
[index
].bitness
= bitness
;
103 strncpy(syscall_table
[index
].name
, name
,
104 sizeof(syscall_table
[index
].name
));
106 DBG("Syscall name '%s' at index %" PRIu32 " of bitness %u",
107 syscall_table[index].name,
108 syscall_table[index].index,
109 syscall_table[index].bitness);
113 syscall_table_nb_entry
= index
;
120 PERROR("syscall list fclose");
127 PERROR("syscall list close");
135 * Helper function for the list syscalls command that empty the temporary
136 * syscall hashtable used to track duplicate between 32 and 64 bit arch.
138 * This empty the hash table and destroys it after. After this, the pointer is
139 * unsuable. RCU read side lock MUST be acquired before calling this.
141 static void destroy_syscall_ht(struct lttng_ht
*ht
)
143 struct lttng_ht_iter iter
;
144 struct syscall
*ksyscall
;
146 DBG3("Destroying syscall hash table.");
152 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, ksyscall
, node
.node
) {
155 ret
= lttng_ht_del(ht
, &iter
);
163 * Allocate the given hashtable pointer.
165 * Return 0 on success else a negative LTTNG error value.
167 static int init_syscall_ht(struct lttng_ht
**ht
)
171 *ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
173 ret
= -LTTNG_ERR_NOMEM
;
182 * Lookup a syscall in the given hash table by name.
184 * Return syscall object if found or else NULL.
186 static struct syscall
*lookup_syscall(struct lttng_ht
*ht
, const char *name
)
188 struct lttng_ht_node_str
*node
;
189 struct lttng_ht_iter iter
;
190 struct syscall
*ksyscall
= NULL
;
195 lttng_ht_lookup(ht
, (void *) name
, &iter
);
196 node
= lttng_ht_iter_get_node_str(&iter
);
198 ksyscall
= caa_container_of(node
, struct syscall
, node
);
205 * Using the given syscall object in the events array with the bitness of the
206 * syscall at index in the syscall table.
208 static void update_event_syscall_bitness(struct lttng_event
*events
,
209 unsigned int index
, unsigned int syscall_index
)
213 if (syscall_table
[index
].bitness
== 32) {
214 events
[syscall_index
].flags
|= LTTNG_EVENT_FLAG_SYSCALL_32
;
216 events
[syscall_index
].flags
|= LTTNG_EVENT_FLAG_SYSCALL_64
;
221 * Allocate and initialize syscall object and add it to the given hashtable.
223 * Return 0 on success else -LTTNG_ERR_NOMEM.
225 static int add_syscall_to_ht(struct lttng_ht
*ht
, unsigned int index
,
226 unsigned int syscall_index
)
229 struct syscall
*ksyscall
;
233 ksyscall
= zmalloc(sizeof(*ksyscall
));
235 ret
= -LTTNG_ERR_NOMEM
;
239 strncpy(ksyscall
->name
, syscall_table
[index
].name
,
240 sizeof(ksyscall
->name
));
241 ksyscall
->bitness
= syscall_table
[index
].bitness
;
242 ksyscall
->index
= syscall_index
;
243 lttng_ht_node_init_str(&ksyscall
->node
, ksyscall
->name
);
244 lttng_ht_add_unique_str(ht
, &ksyscall
->node
);
252 * List syscalls present in the kernel syscall global array, allocate and
253 * populate the events structure with them. Skip the empty syscall name.
255 * Return the number of entries in the array else a negative value.
257 ssize_t
syscall_table_list(struct lttng_event
**_events
)
261 struct lttng_event
*events
;
262 /* Hash table used to filter duplicate out. */
263 struct lttng_ht
*syscalls_ht
= NULL
;
267 DBG("Syscall table listing.");
272 * Allocate at least the number of total syscall we have even if some of
273 * them might not be valid. The count below will make sure to return the
274 * right size of the events array.
276 events
= zmalloc(syscall_table_nb_entry
* sizeof(*events
));
278 PERROR("syscall table list zmalloc");
279 ret
= -LTTNG_ERR_NOMEM
;
283 ret
= init_syscall_ht(&syscalls_ht
);
288 for (i
= 0; i
< syscall_table_nb_entry
; i
++) {
289 struct syscall
*ksyscall
;
291 /* Skip empty syscalls. */
292 if (*syscall_table
[i
].name
== '\0') {
296 ksyscall
= lookup_syscall(syscalls_ht
, syscall_table
[i
].name
);
298 update_event_syscall_bitness(events
, i
, ksyscall
->index
);
302 ret
= add_syscall_to_ht(syscalls_ht
, i
, index
);
307 /* Copy the event information in the event's array. */
308 strncpy(events
[index
].name
, syscall_table
[i
].name
,
309 sizeof(events
[index
].name
));
310 update_event_syscall_bitness(events
, i
, index
);
311 events
[index
].type
= LTTNG_EVENT_SYSCALL
;
312 /* This makes the command line not print the enabled/disabled field. */
313 events
[index
].enabled
= -1;
317 destroy_syscall_ht(syscalls_ht
);
323 destroy_syscall_ht(syscalls_ht
);
330 * Add enabled syscall to the events list using the given kernel channel.
332 * Return the number of entry of the events array that is different from size
333 * if the array grows. On error, return negative value and events is untouched.
335 ssize_t
syscall_list_channel(struct ltt_kernel_channel
*kchan
,
336 struct lttng_event
**_events
, size_t size
)
343 struct lttng_event
*events
= NULL
;
344 /* Hash table used to filter duplicate out. */
345 struct lttng_ht
*syscalls_ht
= NULL
;
349 /* Get syscall mask from the kernel tracer. */
350 err
= kernel_syscall_mask(kchan
->fd
, &mask
, &len
);
356 ret
= init_syscall_ht(&syscalls_ht
);
361 count
= new_size
= size
;
364 for (i
= 0; i
< len
; i
++) {
366 struct syscall
*ksyscall
;
368 bitfield_read_be(mask
, unsigned char, i
, 1, &val
);
370 /* Syscall is disabled, continue the loop. */
374 /* Skip empty syscall. */
375 if (*syscall_table
[i
].name
== '\0') {
379 /* Syscall is enabled thus add it to the events list. */
381 if (count
>= new_size
) {
382 struct lttng_event
*new_events
;
384 /* Get the maximum here since count can be 0. */
385 new_size
= max(count
<< 1, 1);
386 DBG3("Listing syscall realloc events array from %zu to %zu", count
,
388 new_events
= realloc(events
, new_size
* sizeof(*new_events
));
390 PERROR("realloc kernel events list");
394 memset(new_events
+ count
, 0,
395 (new_size
- count
) * sizeof(*new_events
));
399 ksyscall
= lookup_syscall(syscalls_ht
, syscall_table
[i
].name
);
401 update_event_syscall_bitness(events
, i
, ksyscall
->index
);
405 ret
= add_syscall_to_ht(syscalls_ht
, i
, count
);
410 update_event_syscall_bitness(events
, i
, count
);
411 strncpy(events
[count
].name
, syscall_table
[i
].name
,
412 sizeof(events
[count
].name
));
413 events
[count
].enabled
= 1;
414 events
[count
].type
= LTTNG_KERNEL_SYSCALL
;
423 destroy_syscall_ht(syscalls_ht
);