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
> (SIZE_MAX
/ sizeof(*new_list
))) {
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 if (lttng_strncpy(syscall_table
[index
].name
, name
,
112 sizeof(syscall_table
[index
].name
))) {
115 syscall_table
= NULL
;
119 DBG("Syscall name '%s' at index %" PRIu32 " of bitness %u",
120 syscall_table[index].name,
121 syscall_table[index].index,
122 syscall_table[index].bitness);
126 syscall_table_nb_entry
= index
;
133 PERROR("syscall list fclose");
140 PERROR("syscall list close");
148 * Helper function for the list syscalls command that empty the temporary
149 * syscall hashtable used to track duplicate between 32 and 64 bit arch.
151 * This empty the hash table and destroys it after. After this, the pointer is
152 * unsuable. RCU read side lock MUST be acquired before calling this.
154 static void destroy_syscall_ht(struct lttng_ht
*ht
)
156 struct lttng_ht_iter iter
;
157 struct syscall
*ksyscall
;
159 DBG3("Destroying syscall hash table.");
165 cds_lfht_for_each_entry(ht
->ht
, &iter
.iter
, ksyscall
, node
.node
) {
168 ret
= lttng_ht_del(ht
, &iter
);
176 * Allocate the given hashtable pointer.
178 * Return 0 on success else a negative LTTNG error value.
180 static int init_syscall_ht(struct lttng_ht
**ht
)
184 *ht
= lttng_ht_new(0, LTTNG_HT_TYPE_STRING
);
186 ret
= -LTTNG_ERR_NOMEM
;
195 * Lookup a syscall in the given hash table by name.
197 * Return syscall object if found or else NULL.
199 static struct syscall
*lookup_syscall(struct lttng_ht
*ht
, const char *name
)
201 struct lttng_ht_node_str
*node
;
202 struct lttng_ht_iter iter
;
203 struct syscall
*ksyscall
= NULL
;
208 lttng_ht_lookup(ht
, (void *) name
, &iter
);
209 node
= lttng_ht_iter_get_node_str(&iter
);
211 ksyscall
= caa_container_of(node
, struct syscall
, node
);
218 * Using the given syscall object in the events array with the bitness of the
219 * syscall at index in the syscall table.
221 static void update_event_syscall_bitness(struct lttng_event
*events
,
222 unsigned int index
, unsigned int syscall_index
)
226 if (syscall_table
[index
].bitness
== 32) {
227 events
[syscall_index
].flags
|= LTTNG_EVENT_FLAG_SYSCALL_32
;
229 events
[syscall_index
].flags
|= LTTNG_EVENT_FLAG_SYSCALL_64
;
234 * Allocate and initialize syscall object and add it to the given hashtable.
236 * Return 0 on success else -LTTNG_ERR_NOMEM.
238 static int add_syscall_to_ht(struct lttng_ht
*ht
, unsigned int index
,
239 unsigned int syscall_index
)
242 struct syscall
*ksyscall
;
246 ksyscall
= zmalloc(sizeof(*ksyscall
));
248 ret
= -LTTNG_ERR_NOMEM
;
252 strncpy(ksyscall
->name
, syscall_table
[index
].name
,
253 sizeof(ksyscall
->name
));
254 ksyscall
->bitness
= syscall_table
[index
].bitness
;
255 ksyscall
->index
= syscall_index
;
256 lttng_ht_node_init_str(&ksyscall
->node
, ksyscall
->name
);
257 lttng_ht_add_unique_str(ht
, &ksyscall
->node
);
265 * List syscalls present in the kernel syscall global array, allocate and
266 * populate the events structure with them. Skip the empty syscall name.
268 * Return the number of entries in the array else a negative value.
270 ssize_t
syscall_table_list(struct lttng_event
**_events
)
274 struct lttng_event
*events
;
275 /* Hash table used to filter duplicate out. */
276 struct lttng_ht
*syscalls_ht
= NULL
;
280 DBG("Syscall table listing.");
285 * Allocate at least the number of total syscall we have even if some of
286 * them might not be valid. The count below will make sure to return the
287 * right size of the events array.
289 events
= zmalloc(syscall_table_nb_entry
* sizeof(*events
));
291 PERROR("syscall table list zmalloc");
292 ret
= -LTTNG_ERR_NOMEM
;
296 ret
= init_syscall_ht(&syscalls_ht
);
301 for (i
= 0; i
< syscall_table_nb_entry
; i
++) {
302 struct syscall
*ksyscall
;
304 /* Skip empty syscalls. */
305 if (*syscall_table
[i
].name
== '\0') {
309 ksyscall
= lookup_syscall(syscalls_ht
, syscall_table
[i
].name
);
311 update_event_syscall_bitness(events
, i
, ksyscall
->index
);
315 ret
= add_syscall_to_ht(syscalls_ht
, i
, index
);
320 /* Copy the event information in the event's array. */
321 strncpy(events
[index
].name
, syscall_table
[i
].name
,
322 sizeof(events
[index
].name
));
323 update_event_syscall_bitness(events
, i
, index
);
324 events
[index
].type
= LTTNG_EVENT_SYSCALL
;
325 /* This makes the command line not print the enabled/disabled field. */
326 events
[index
].enabled
= -1;
330 destroy_syscall_ht(syscalls_ht
);
336 destroy_syscall_ht(syscalls_ht
);