vscode: Add configurations to run the executables under the debugger
[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 "kernel.hpp"
10 #include "lttng-sessiond.hpp"
11 #include "lttng-syscall.hpp"
12 #include "utils.hpp"
13
14 #include <common/common.hpp>
15 #include <common/kernel-ctl/kernel-ctl.hpp>
16 #include <common/urcu.hpp>
17
18 #include <stdbool.h>
19
20 /* Global syscall table. */
21 struct syscall *syscall_table;
22
23 /* Number of entry in the syscall table. */
24 static 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
30 * value is returned.
31 */
32 int syscall_init_table(int tracer_fd)
33 {
34 int ret, fd, err;
35 size_t nbmem;
36 FILE *fp;
37 /* Syscall data from the kernel. */
38 size_t index = 0;
39 bool at_least_one_syscall = false;
40 uint32_t bitness;
41 char name[SYSCALL_NAME_LEN];
42
43 #if (SYSCALL_NAME_LEN == 255)
44 #define SYSCALL_NAME_LEN_SCANF_IS_A_BROKEN_API "254"
45 #endif
46
47 DBG3("Syscall init system call table");
48
49 fd = kernctl_syscall_list(tracer_fd);
50 if (fd < 0) {
51 ret = fd;
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;
64 syscall_table = calloc<struct syscall>(nbmem);
65 if (!syscall_table) {
66 ret = -errno;
67 PERROR("syscall list zmalloc");
68 goto error;
69 }
70
71 while (fscanf(fp,
72 "syscall { index = %zu; \
73 name = %" SYSCALL_NAME_LEN_SCANF_IS_A_BROKEN_API "[^;]; \
74 bitness = %u; };\n",
75 &index,
76 name,
77 &bitness) == 3) {
78 at_least_one_syscall = true;
79 if (index >= nbmem) {
80 struct syscall *new_list;
81 size_t new_nbmem;
82
83 /* Double memory size. */
84 new_nbmem = std::max(index + 1, nbmem << 1);
85 if (new_nbmem > (SIZE_MAX / sizeof(*new_list))) {
86 /* Overflow, stop everything, something went really wrong. */
87 ERR("Syscall listing memory size overflow. Stopping");
88 free(syscall_table);
89 syscall_table = nullptr;
90 ret = -EINVAL;
91 goto error;
92 }
93
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));
97 if (!new_list) {
98 ret = -errno;
99 PERROR("syscall list realloc");
100 goto error;
101 }
102
103 /* Zero out the new memory. */
104 memset(new_list + nbmem, 0, (new_nbmem - nbmem) * sizeof(*new_list));
105 nbmem = new_nbmem;
106 syscall_table = new_list;
107 }
108 syscall_table[index].index = index;
109 syscall_table[index].bitness = bitness;
110 if (lttng_strncpy(
111 syscall_table[index].name, name, sizeof(syscall_table[index].name))) {
112 ret = -EINVAL;
113 free(syscall_table);
114 syscall_table = nullptr;
115 goto error;
116 }
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
125 /* Index starts at 0. */
126 if (at_least_one_syscall) {
127 syscall_table_nb_entry = index + 1;
128 }
129
130 ret = 0;
131
132 error:
133 err = fclose(fp);
134 if (err) {
135 PERROR("syscall list fclose");
136 }
137 return ret;
138
139 error_fp:
140 err = close(fd);
141 if (err) {
142 PERROR("syscall list close");
143 }
144
145 error_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
154 * unsuable. RCU read side lock MUST NOT be acquired before calling this.
155 */
156 static 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
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;
172
173 ret = lttng_ht_del(ht, &iter);
174 LTTNG_ASSERT(!ret);
175 free(ksyscall);
176 }
177 }
178
179 lttng_ht_destroy(ht);
180 }
181
182 /*
183 * Allocate the given hashtable pointer.
184 *
185 * Return 0 on success else a negative LTTNG error value.
186 */
187 static 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 *
204 * RCU read lock MUST be acquired by the callers of this function.
205 *
206 * Return syscall object if found or else NULL.
207 */
208 static 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;
212 struct syscall *ksyscall = nullptr;
213
214 LTTNG_ASSERT(ht);
215 LTTNG_ASSERT(name);
216
217 lttng_ht_lookup(ht, (void *) name, &iter);
218 node = lttng_ht_iter_get_node_str(&iter);
219 if (node) {
220 ksyscall = lttng::utils::container_of(node, &syscall::node);
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 */
230 static void update_event_syscall_bitness(struct lttng_event *events,
231 unsigned int index,
232 unsigned int syscall_index)
233 {
234 LTTNG_ASSERT(events);
235
236 if (syscall_table[index].bitness == 32) {
237 events[syscall_index].flags = (lttng_event_flag) (events[syscall_index].flags |
238 LTTNG_EVENT_FLAG_SYSCALL_32);
239 } else {
240 events[syscall_index].flags = (lttng_event_flag) (events[syscall_index].flags |
241 LTTNG_EVENT_FLAG_SYSCALL_64);
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 */
250 static int add_syscall_to_ht(struct lttng_ht *ht, unsigned int index, unsigned int syscall_index)
251 {
252 int ret;
253 struct syscall *ksyscall;
254
255 LTTNG_ASSERT(ht);
256
257 ksyscall = zmalloc<struct syscall>();
258 if (!ksyscall) {
259 ret = -LTTNG_ERR_NOMEM;
260 goto error;
261 }
262
263 strncpy(ksyscall->name, syscall_table[index].name, sizeof(ksyscall->name));
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
270 error:
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 */
280 ssize_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. */
286 struct lttng_ht *syscalls_ht = nullptr;
287
288 LTTNG_ASSERT(_events);
289
290 DBG("Syscall table listing.");
291
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 */
297 events = calloc<lttng_event>(syscall_table_nb_entry);
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++) {
310 /* Skip empty syscalls. */
311 if (*syscall_table[i].name == '\0') {
312 continue;
313 }
314
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 }
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. */
332 strncpy(events[index].name, syscall_table[i].name, sizeof(events[index].name));
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;
342 return index;
343
344 error:
345 destroy_syscall_ht(syscalls_ht);
346 free(events);
347 return ret;
348 }
This page took 0.035777 seconds and 4 git commands to generate.