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