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