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