bin: compile lttng-sessiond as C++
[lttng-tools.git] / src / bin / lttng-sessiond / lttng-syscall.cpp
1 /*
2 * Copyright (C) 2014 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <stdbool.h>
10
11 #include <common/common.h>
12 #include <common/kernel-ctl/kernel-ctl.h>
13
14 #include "lttng-sessiond.h"
15 #include "kernel.h"
16 #include "lttng-syscall.h"
17 #include "utils.h"
18
19 /* Global syscall table. */
20 struct syscall *syscall_table;
21
22 /* Number of entry in the syscall table. */
23 static size_t syscall_table_nb_entry;
24
25 /*
26 * Populate the system call table using the kernel tracer.
27 *
28 * Return 0 on success and the syscall table is allocated. On error, a negative
29 * value is returned.
30 */
31 int syscall_init_table(int tracer_fd)
32 {
33 int ret, fd, err;
34 size_t nbmem;
35 FILE *fp;
36 /* Syscall data from the kernel. */
37 size_t index = 0;
38 bool at_least_one_syscall = false;
39 uint32_t bitness;
40 char name[SYSCALL_NAME_LEN];
41
42 #if (SYSCALL_NAME_LEN == 255)
43 #define SYSCALL_NAME_LEN_SCANF_IS_A_BROKEN_API "254"
44 #endif
45
46 DBG3("Syscall init system call table");
47
48 fd = kernctl_syscall_list(tracer_fd);
49 if (fd < 0) {
50 ret = fd;
51 PERROR("kernelctl syscall list");
52 goto error_ioctl;
53 }
54
55 fp = fdopen(fd, "r");
56 if (!fp) {
57 ret = -errno;
58 PERROR("syscall list fdopen");
59 goto error_fp;
60 }
61
62 nbmem = SYSCALL_TABLE_INIT_SIZE;
63 syscall_table = (struct syscall *) zmalloc(sizeof(struct syscall) * nbmem);
64 if (!syscall_table) {
65 ret = -errno;
66 PERROR("syscall list zmalloc");
67 goto error;
68 }
69
70 while (fscanf(fp,
71 "syscall { index = %zu; \
72 name = %" SYSCALL_NAME_LEN_SCANF_IS_A_BROKEN_API "[^;]; \
73 bitness = %u; };\n",
74 &index, name, &bitness) == 3) {
75 at_least_one_syscall = true;
76 if (index >= nbmem) {
77 struct syscall *new_list;
78 size_t new_nbmem;
79
80 /* Double memory size. */
81 new_nbmem = std::max(index + 1, nbmem << 1);
82 if (new_nbmem > (SIZE_MAX / sizeof(*new_list))) {
83 /* Overflow, stop everything, something went really wrong. */
84 ERR("Syscall listing memory size overflow. Stopping");
85 free(syscall_table);
86 syscall_table = NULL;
87 ret = -EINVAL;
88 goto error;
89 }
90
91 DBG("Reallocating syscall table from %zu to %zu entries", nbmem,
92 new_nbmem);
93 new_list = (struct syscall *) realloc(syscall_table, new_nbmem * sizeof(*new_list));
94 if (!new_list) {
95 ret = -errno;
96 PERROR("syscall list realloc");
97 goto error;
98 }
99
100 /* Zero out the new memory. */
101 memset(new_list + nbmem, 0,
102 (new_nbmem - nbmem) * sizeof(*new_list));
103 nbmem = new_nbmem;
104 syscall_table = new_list;
105 }
106 syscall_table[index].index = index;
107 syscall_table[index].bitness = bitness;
108 if (lttng_strncpy(syscall_table[index].name, name,
109 sizeof(syscall_table[index].name))) {
110 ret = -EINVAL;
111 free(syscall_table);
112 syscall_table = NULL;
113 goto error;
114 }
115 /*
116 DBG("Syscall name '%s' at index %" PRIu32 " of bitness %u",
117 syscall_table[index].name,
118 syscall_table[index].index,
119 syscall_table[index].bitness);
120 */
121 }
122
123 /* Index starts at 0. */
124 if (at_least_one_syscall) {
125 syscall_table_nb_entry = index + 1;
126 }
127
128 ret = 0;
129
130 error:
131 err = fclose(fp);
132 if (err) {
133 PERROR("syscall list fclose");
134 }
135 return ret;
136
137 error_fp:
138 err = close(fd);
139 if (err) {
140 PERROR("syscall list close");
141 }
142
143 error_ioctl:
144 return ret;
145 }
146
147 /*
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.
150 *
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.
153 */
154 static void destroy_syscall_ht(struct lttng_ht *ht)
155 {
156 struct lttng_ht_iter iter;
157 struct syscall *ksyscall;
158
159 DBG3("Destroying syscall hash table.");
160
161 if (!ht) {
162 return;
163 }
164
165 cds_lfht_for_each_entry(ht->ht, &iter.iter, ksyscall, node.node) {
166 int ret;
167
168 ret = lttng_ht_del(ht, &iter);
169 LTTNG_ASSERT(!ret);
170 free(ksyscall);
171 }
172 ht_cleanup_push(ht);
173 }
174
175 /*
176 * Allocate the given hashtable pointer.
177 *
178 * Return 0 on success else a negative LTTNG error value.
179 */
180 static int init_syscall_ht(struct lttng_ht **ht)
181 {
182 int ret;
183
184 *ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
185 if (!*ht) {
186 ret = -LTTNG_ERR_NOMEM;
187 } else {
188 ret = 0;
189 }
190
191 return ret;
192 }
193
194 /*
195 * Lookup a syscall in the given hash table by name.
196 *
197 * Return syscall object if found or else NULL.
198 */
199 static struct syscall *lookup_syscall(struct lttng_ht *ht, const char *name)
200 {
201 struct lttng_ht_node_str *node;
202 struct lttng_ht_iter iter;
203 struct syscall *ksyscall = NULL;
204
205 LTTNG_ASSERT(ht);
206 LTTNG_ASSERT(name);
207
208 lttng_ht_lookup(ht, (void *) name, &iter);
209 node = lttng_ht_iter_get_node_str(&iter);
210 if (node) {
211 ksyscall = caa_container_of(node, struct syscall, node);
212 }
213
214 return ksyscall;
215 }
216
217 /*
218 * Using the given syscall object in the events array with the bitness of the
219 * syscall at index in the syscall table.
220 */
221 static void update_event_syscall_bitness(struct lttng_event *events,
222 unsigned int index, unsigned int syscall_index)
223 {
224 LTTNG_ASSERT(events);
225
226 if (syscall_table[index].bitness == 32) {
227 events[syscall_index].flags = (lttng_event_flag) (events[syscall_index].flags | LTTNG_EVENT_FLAG_SYSCALL_32);
228 } else {
229 events[syscall_index].flags = (lttng_event_flag) (events[syscall_index].flags | LTTNG_EVENT_FLAG_SYSCALL_64);
230 }
231 }
232
233 /*
234 * Allocate and initialize syscall object and add it to the given hashtable.
235 *
236 * Return 0 on success else -LTTNG_ERR_NOMEM.
237 */
238 static int add_syscall_to_ht(struct lttng_ht *ht, unsigned int index,
239 unsigned int syscall_index)
240 {
241 int ret;
242 struct syscall *ksyscall;
243
244 LTTNG_ASSERT(ht);
245
246 ksyscall = (struct syscall *) zmalloc(sizeof(*ksyscall));
247 if (!ksyscall) {
248 ret = -LTTNG_ERR_NOMEM;
249 goto error;
250 }
251
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);
258 ret = 0;
259
260 error:
261 return ret;
262 }
263
264 /*
265 * List syscalls present in the kernel syscall global array, allocate and
266 * populate the events structure with them. Skip the empty syscall name.
267 *
268 * Return the number of entries in the array else a negative value.
269 */
270 ssize_t syscall_table_list(struct lttng_event **_events)
271 {
272 int i, index = 0;
273 ssize_t ret;
274 struct lttng_event *events;
275 /* Hash table used to filter duplicate out. */
276 struct lttng_ht *syscalls_ht = NULL;
277
278 LTTNG_ASSERT(_events);
279
280 DBG("Syscall table listing.");
281
282 rcu_read_lock();
283
284 /*
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.
288 */
289 events = (lttng_event *) zmalloc(syscall_table_nb_entry * sizeof(*events));
290 if (!events) {
291 PERROR("syscall table list zmalloc");
292 ret = -LTTNG_ERR_NOMEM;
293 goto error;
294 }
295
296 ret = init_syscall_ht(&syscalls_ht);
297 if (ret < 0) {
298 goto error;
299 }
300
301 for (i = 0; i < syscall_table_nb_entry; i++) {
302 struct syscall *ksyscall;
303
304 /* Skip empty syscalls. */
305 if (*syscall_table[i].name == '\0') {
306 continue;
307 }
308
309 ksyscall = lookup_syscall(syscalls_ht, syscall_table[i].name);
310 if (ksyscall) {
311 update_event_syscall_bitness(events, i, ksyscall->index);
312 continue;
313 }
314
315 ret = add_syscall_to_ht(syscalls_ht, i, index);
316 if (ret < 0) {
317 goto error;
318 }
319
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;
327 index++;
328 }
329
330 destroy_syscall_ht(syscalls_ht);
331 *_events = events;
332 rcu_read_unlock();
333 return index;
334
335 error:
336 destroy_syscall_ht(syscalls_ht);
337 free(events);
338 rcu_read_unlock();
339 return ret;
340 }
This page took 0.035273 seconds and 4 git commands to generate.