Build fix: rpath of test libraries results in non-reproducible build
[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
98123e1c
JR
9#include <stdbool.h>
10
c9e313bc
SM
11#include <common/common.hpp>
12#include <common/kernel-ctl/kernel-ctl.hpp>
834978fd 13
c9e313bc
SM
14#include "lttng-sessiond.hpp"
15#include "kernel.hpp"
16#include "lttng-syscall.hpp"
17#include "utils.hpp"
834978fd
DG
18
19/* Global syscall table. */
20struct syscall *syscall_table;
21
22/* Number of entry in the syscall table. */
23static 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
1f47715a 29 * value is returned.
834978fd 30 */
7d268848 31int syscall_init_table(int tracer_fd)
834978fd
DG
32{
33 int ret, fd, err;
34 size_t nbmem;
35 FILE *fp;
36 /* Syscall data from the kernel. */
98123e1c
JR
37 size_t index = 0;
38 bool at_least_one_syscall = false;
834978fd
DG
39 uint32_t bitness;
40 char name[SYSCALL_NAME_LEN];
41
9c7134c0
SM
42#if (SYSCALL_NAME_LEN == 255)
43#define SYSCALL_NAME_LEN_SCANF_IS_A_BROKEN_API "254"
44#endif
45
834978fd
DG
46 DBG3("Syscall init system call table");
47
7d268848 48 fd = kernctl_syscall_list(tracer_fd);
834978fd 49 if (fd < 0) {
32af2c95 50 ret = fd;
834978fd
DG
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;
64803277 63 syscall_table = calloc<struct syscall>(nbmem);
834978fd
DG
64 if (!syscall_table) {
65 ret = -errno;
66 PERROR("syscall list zmalloc");
67 goto error;
68 }
69
70 while (fscanf(fp,
97282ce2 71 "syscall { index = %zu; \
9c7134c0 72 name = %" SYSCALL_NAME_LEN_SCANF_IS_A_BROKEN_API "[^;]; \
834978fd
DG
73 bitness = %u; };\n",
74 &index, name, &bitness) == 3) {
98123e1c
JR
75 at_least_one_syscall = true;
76 if (index >= nbmem) {
834978fd
DG
77 struct syscall *new_list;
78 size_t new_nbmem;
79
80 /* Double memory size. */
7966af57 81 new_nbmem = std::max(index + 1, nbmem << 1);
ec93758c 82 if (new_nbmem > (SIZE_MAX / sizeof(*new_list))) {
1f47715a
DG
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 }
834978fd
DG
90
91 DBG("Reallocating syscall table from %zu to %zu entries", nbmem,
92 new_nbmem);
7966af57 93 new_list = (struct syscall *) realloc(syscall_table, new_nbmem * sizeof(*new_list));
834978fd
DG
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;
39e3c47a
MD
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 }
834978fd
DG
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
98123e1c
JR
123 /* Index starts at 0. */
124 if (at_least_one_syscall) {
125 syscall_table_nb_entry = index + 1;
126 }
834978fd
DG
127
128 ret = 0;
129
130error:
131 err = fclose(fp);
132 if (err) {
133 PERROR("syscall list fclose");
134 }
135 return ret;
136
137error_fp:
138 err = close(fd);
139 if (err) {
140 PERROR("syscall list close");
141 }
142
143error_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 */
154static void destroy_syscall_ht(struct lttng_ht *ht)
155{
156 struct lttng_ht_iter iter;
157 struct syscall *ksyscall;
158
48b7cdc2
FD
159 ASSERT_RCU_READ_LOCKED();
160
834978fd
DG
161 DBG3("Destroying syscall hash table.");
162
163 if (!ht) {
164 return;
165 }
166
167 cds_lfht_for_each_entry(ht->ht, &iter.iter, ksyscall, node.node) {
168 int ret;
169
170 ret = lttng_ht_del(ht, &iter);
a0377dfe 171 LTTNG_ASSERT(!ret);
834978fd
DG
172 free(ksyscall);
173 }
3c339053 174 lttng_ht_destroy(ht);
834978fd
DG
175}
176
177/*
178 * Allocate the given hashtable pointer.
179 *
180 * Return 0 on success else a negative LTTNG error value.
181 */
182static int init_syscall_ht(struct lttng_ht **ht)
183{
184 int ret;
185
186 *ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
187 if (!*ht) {
188 ret = -LTTNG_ERR_NOMEM;
189 } else {
190 ret = 0;
191 }
192
193 return ret;
194}
195
196/*
197 * Lookup a syscall in the given hash table by name.
198 *
199 * Return syscall object if found or else NULL.
200 */
201static struct syscall *lookup_syscall(struct lttng_ht *ht, const char *name)
202{
203 struct lttng_ht_node_str *node;
204 struct lttng_ht_iter iter;
205 struct syscall *ksyscall = NULL;
206
a0377dfe
FD
207 LTTNG_ASSERT(ht);
208 LTTNG_ASSERT(name);
834978fd
DG
209
210 lttng_ht_lookup(ht, (void *) name, &iter);
211 node = lttng_ht_iter_get_node_str(&iter);
212 if (node) {
0114db0e 213 ksyscall = lttng::utils::container_of(node, &syscall::node);
834978fd
DG
214 }
215
216 return ksyscall;
217}
218
219/*
220 * Using the given syscall object in the events array with the bitness of the
221 * syscall at index in the syscall table.
222 */
223static void update_event_syscall_bitness(struct lttng_event *events,
224 unsigned int index, unsigned int syscall_index)
225{
a0377dfe 226 LTTNG_ASSERT(events);
834978fd
DG
227
228 if (syscall_table[index].bitness == 32) {
7966af57 229 events[syscall_index].flags = (lttng_event_flag) (events[syscall_index].flags | LTTNG_EVENT_FLAG_SYSCALL_32);
834978fd 230 } else {
7966af57 231 events[syscall_index].flags = (lttng_event_flag) (events[syscall_index].flags | LTTNG_EVENT_FLAG_SYSCALL_64);
834978fd
DG
232 }
233}
234
235/*
236 * Allocate and initialize syscall object and add it to the given hashtable.
237 *
238 * Return 0 on success else -LTTNG_ERR_NOMEM.
239 */
240static int add_syscall_to_ht(struct lttng_ht *ht, unsigned int index,
241 unsigned int syscall_index)
242{
243 int ret;
244 struct syscall *ksyscall;
245
a0377dfe 246 LTTNG_ASSERT(ht);
834978fd 247
64803277 248 ksyscall = zmalloc<struct syscall>();
834978fd
DG
249 if (!ksyscall) {
250 ret = -LTTNG_ERR_NOMEM;
251 goto error;
252 }
253
254 strncpy(ksyscall->name, syscall_table[index].name,
255 sizeof(ksyscall->name));
256 ksyscall->bitness = syscall_table[index].bitness;
257 ksyscall->index = syscall_index;
258 lttng_ht_node_init_str(&ksyscall->node, ksyscall->name);
259 lttng_ht_add_unique_str(ht, &ksyscall->node);
260 ret = 0;
261
262error:
263 return ret;
264}
265
266/*
267 * List syscalls present in the kernel syscall global array, allocate and
268 * populate the events structure with them. Skip the empty syscall name.
269 *
270 * Return the number of entries in the array else a negative value.
271 */
272ssize_t syscall_table_list(struct lttng_event **_events)
273{
274 int i, index = 0;
275 ssize_t ret;
276 struct lttng_event *events;
277 /* Hash table used to filter duplicate out. */
278 struct lttng_ht *syscalls_ht = NULL;
279
a0377dfe 280 LTTNG_ASSERT(_events);
834978fd
DG
281
282 DBG("Syscall table listing.");
283
284 rcu_read_lock();
285
286 /*
287 * Allocate at least the number of total syscall we have even if some of
288 * them might not be valid. The count below will make sure to return the
289 * right size of the events array.
290 */
64803277 291 events = calloc<lttng_event>(syscall_table_nb_entry);
834978fd
DG
292 if (!events) {
293 PERROR("syscall table list zmalloc");
294 ret = -LTTNG_ERR_NOMEM;
295 goto error;
296 }
297
298 ret = init_syscall_ht(&syscalls_ht);
299 if (ret < 0) {
300 goto error;
301 }
302
303 for (i = 0; i < syscall_table_nb_entry; i++) {
304 struct syscall *ksyscall;
305
306 /* Skip empty syscalls. */
307 if (*syscall_table[i].name == '\0') {
308 continue;
309 }
310
311 ksyscall = lookup_syscall(syscalls_ht, syscall_table[i].name);
312 if (ksyscall) {
313 update_event_syscall_bitness(events, i, ksyscall->index);
314 continue;
315 }
316
317 ret = add_syscall_to_ht(syscalls_ht, i, index);
318 if (ret < 0) {
319 goto error;
320 }
321
322 /* Copy the event information in the event's array. */
323 strncpy(events[index].name, syscall_table[i].name,
324 sizeof(events[index].name));
325 update_event_syscall_bitness(events, i, index);
326 events[index].type = LTTNG_EVENT_SYSCALL;
327 /* This makes the command line not print the enabled/disabled field. */
328 events[index].enabled = -1;
329 index++;
330 }
331
332 destroy_syscall_ht(syscalls_ht);
333 *_events = events;
334 rcu_read_unlock();
335 return index;
336
337error:
338 destroy_syscall_ht(syscalls_ht);
339 free(events);
340 rcu_read_unlock();
341 return ret;
342}
This page took 0.091989 seconds and 4 git commands to generate.