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