Add syscall listing support
[lttng-tools.git] / src / bin / lttng-sessiond / syscall.c
1 /*
2 * Copyright (C) 2014 - David Goulet <dgoulet@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18 #define _GNU_SOURCE
19 #include <common/bitfield.h>
20 #include <common/common.h>
21 #include <common/kernel-ctl/kernel-ctl.h>
22
23 #include "lttng-sessiond.h"
24 #include "kernel.h"
25 #include "syscall.h"
26 #include "utils.h"
27
28 /* Global syscall table. */
29 struct syscall *syscall_table;
30
31 /* Number of entry in the syscall table. */
32 static size_t syscall_table_nb_entry;
33
34 /*
35 * Populate the system call table using the kernel tracer.
36 *
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.
39 */
40 int syscall_init_table(void)
41 {
42 int ret, fd, err;
43 size_t nbmem;
44 FILE *fp;
45 /* Syscall data from the kernel. */
46 size_t index;
47 uint32_t bitness;
48 char name[SYSCALL_NAME_LEN];
49
50 DBG3("Syscall init system call table");
51
52 fd = kernctl_syscall_list(kernel_tracer_fd);
53 if (fd < 0) {
54 ret = -errno;
55 PERROR("kernelctl syscall list");
56 goto error_ioctl;
57 }
58
59 fp = fdopen(fd, "r");
60 if (!fp) {
61 ret = -errno;
62 PERROR("syscall list fdopen");
63 goto error_fp;
64 }
65
66 nbmem = SYSCALL_TABLE_INIT_SIZE;
67 syscall_table = zmalloc(sizeof(struct syscall) * nbmem);
68 if (!syscall_table) {
69 ret = -errno;
70 PERROR("syscall list zmalloc");
71 goto error;
72 }
73
74 while (fscanf(fp,
75 "syscall { index = %lu; \
76 name = %" XSTR(SYSCALL_NAME_LEN) "[^;]; \
77 bitness = %u; };\n",
78 &index, name, &bitness) == 3) {
79 if (index >= nbmem ) {
80 struct syscall *new_list;
81 size_t new_nbmem;
82
83 /* Double memory size. */
84 new_nbmem = index << 1;
85
86 DBG("Reallocating syscall table from %zu to %zu entries", nbmem,
87 new_nbmem);
88 new_list = realloc(syscall_table, new_nbmem * sizeof(*new_list));
89 if (!new_list) {
90 ret = -errno;
91 PERROR("syscall list realloc");
92 goto error;
93 }
94
95 /* Zero out the new memory. */
96 memset(new_list + nbmem, 0,
97 (new_nbmem - nbmem) * sizeof(*new_list));
98 nbmem = new_nbmem;
99 syscall_table = new_list;
100 }
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));
105 /*
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);
110 */
111 }
112
113 syscall_table_nb_entry = index;
114
115 ret = 0;
116
117 error:
118 err = fclose(fp);
119 if (err) {
120 PERROR("syscall list fclose");
121 }
122 return ret;
123
124 error_fp:
125 err = close(fd);
126 if (err) {
127 PERROR("syscall list close");
128 }
129
130 error_ioctl:
131 return ret;
132 }
133
134 /*
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.
137 *
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.
140 */
141 static void destroy_syscall_ht(struct lttng_ht *ht)
142 {
143 struct lttng_ht_iter iter;
144 struct syscall *ksyscall;
145
146 DBG3("Destroying syscall hash table.");
147
148 if (!ht) {
149 return;
150 }
151
152 cds_lfht_for_each_entry(ht->ht, &iter.iter, ksyscall, node.node) {
153 int ret;
154
155 ret = lttng_ht_del(ht, &iter);
156 assert(!ret);
157 free(ksyscall);
158 }
159 ht_cleanup_push(ht);
160 }
161
162 /*
163 * Allocate the given hashtable pointer.
164 *
165 * Return 0 on success else a negative LTTNG error value.
166 */
167 static int init_syscall_ht(struct lttng_ht **ht)
168 {
169 int ret;
170
171 *ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
172 if (!*ht) {
173 ret = -LTTNG_ERR_NOMEM;
174 } else {
175 ret = 0;
176 }
177
178 return ret;
179 }
180
181 /*
182 * Lookup a syscall in the given hash table by name.
183 *
184 * Return syscall object if found or else NULL.
185 */
186 static struct syscall *lookup_syscall(struct lttng_ht *ht, const char *name)
187 {
188 struct lttng_ht_node_str *node;
189 struct lttng_ht_iter iter;
190 struct syscall *ksyscall = NULL;
191
192 assert(ht);
193 assert(name);
194
195 lttng_ht_lookup(ht, (void *) name, &iter);
196 node = lttng_ht_iter_get_node_str(&iter);
197 if (node) {
198 ksyscall = caa_container_of(node, struct syscall, node);
199 }
200
201 return ksyscall;
202 }
203
204 /*
205 * Using the given syscall object in the events array with the bitness of the
206 * syscall at index in the syscall table.
207 */
208 static void update_event_syscall_bitness(struct lttng_event *events,
209 unsigned int index, unsigned int syscall_index)
210 {
211 assert(events);
212
213 if (syscall_table[index].bitness == 32) {
214 events[syscall_index].flags |= LTTNG_EVENT_FLAG_SYSCALL_32;
215 } else {
216 events[syscall_index].flags |= LTTNG_EVENT_FLAG_SYSCALL_64;
217 }
218 }
219
220 /*
221 * Allocate and initialize syscall object and add it to the given hashtable.
222 *
223 * Return 0 on success else -LTTNG_ERR_NOMEM.
224 */
225 static int add_syscall_to_ht(struct lttng_ht *ht, unsigned int index,
226 unsigned int syscall_index)
227 {
228 int ret;
229 struct syscall *ksyscall;
230
231 assert(ht);
232
233 ksyscall = zmalloc(sizeof(*ksyscall));
234 if (!ksyscall) {
235 ret = -LTTNG_ERR_NOMEM;
236 goto error;
237 }
238
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);
245 ret = 0;
246
247 error:
248 return ret;
249 }
250
251 /*
252 * List syscalls present in the kernel syscall global array, allocate and
253 * populate the events structure with them. Skip the empty syscall name.
254 *
255 * Return the number of entries in the array else a negative value.
256 */
257 ssize_t syscall_table_list(struct lttng_event **_events)
258 {
259 int i, index = 0;
260 ssize_t ret;
261 struct lttng_event *events;
262 /* Hash table used to filter duplicate out. */
263 struct lttng_ht *syscalls_ht = NULL;
264
265 assert(_events);
266
267 DBG("Syscall table listing.");
268
269 rcu_read_lock();
270
271 /*
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.
275 */
276 events = zmalloc(syscall_table_nb_entry * sizeof(*events));
277 if (!events) {
278 PERROR("syscall table list zmalloc");
279 ret = -LTTNG_ERR_NOMEM;
280 goto error;
281 }
282
283 ret = init_syscall_ht(&syscalls_ht);
284 if (ret < 0) {
285 goto error;
286 }
287
288 for (i = 0; i < syscall_table_nb_entry; i++) {
289 struct syscall *ksyscall;
290
291 /* Skip empty syscalls. */
292 if (*syscall_table[i].name == '\0') {
293 continue;
294 }
295
296 ksyscall = lookup_syscall(syscalls_ht, syscall_table[i].name);
297 if (ksyscall) {
298 update_event_syscall_bitness(events, i, ksyscall->index);
299 continue;
300 }
301
302 ret = add_syscall_to_ht(syscalls_ht, i, index);
303 if (ret < 0) {
304 goto error;
305 }
306
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;
314 index++;
315 }
316
317 destroy_syscall_ht(syscalls_ht);
318 *_events = events;
319 rcu_read_unlock();
320 return index;
321
322 error:
323 destroy_syscall_ht(syscalls_ht);
324 free(events);
325 rcu_read_unlock();
326 return ret;
327 }
328
329 /*
330 * Add enabled syscall to the events list using the given kernel channel.
331 *
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.
334 */
335 ssize_t syscall_list_channel(struct ltt_kernel_channel *kchan,
336 struct lttng_event **_events, size_t size)
337 {
338 int err, i;
339 size_t new_size;
340 ssize_t ret, count;
341 char *mask = NULL;
342 uint32_t len;
343 struct lttng_event *events = NULL;
344 /* Hash table used to filter duplicate out. */
345 struct lttng_ht *syscalls_ht = NULL;
346
347 assert(kchan);
348
349 /* Get syscall mask from the kernel tracer. */
350 err = kernel_syscall_mask(kchan->fd, &mask, &len);
351 if (err < 0) {
352 ret = err;
353 goto error;
354 }
355
356 ret = init_syscall_ht(&syscalls_ht);
357 if (ret < 0) {
358 goto error;
359 }
360
361 count = new_size = size;
362 events = *_events;
363
364 for (i = 0; i < len; i++) {
365 unsigned char val;
366 struct syscall *ksyscall;
367
368 bitfield_read_be(mask, unsigned char, i, 1, &val);
369 if (!val) {
370 /* Syscall is disabled, continue the loop. */
371 continue;
372 }
373
374 /* Skip empty syscall. */
375 if (*syscall_table[i].name == '\0') {
376 continue;
377 }
378
379 /* Syscall is enabled thus add it to the events list. */
380
381 if (count >= new_size) {
382 struct lttng_event *new_events;
383
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,
387 new_size);
388 new_events = realloc(events, new_size * sizeof(*new_events));
389 if (!new_events) {
390 PERROR("realloc kernel events list");
391 ret = -ENOMEM;
392 goto error;
393 }
394 memset(new_events + count, 0,
395 (new_size - count) * sizeof(*new_events));
396 events = new_events;
397 }
398
399 ksyscall = lookup_syscall(syscalls_ht, syscall_table[i].name);
400 if (ksyscall) {
401 update_event_syscall_bitness(events, i, ksyscall->index);
402 continue;
403 }
404
405 ret = add_syscall_to_ht(syscalls_ht, i, count);
406 if (ret < 0) {
407 goto error;
408 }
409
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;
415 count++;
416 }
417
418 *_events = events;
419
420 return count;
421
422 error:
423 destroy_syscall_ht(syscalls_ht);
424 free(events);
425 return ret;
426 }
This page took 0.039549 seconds and 5 git commands to generate.