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