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