| 1 | /* |
| 2 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "filter.h" |
| 9 | #include <stddef.h> |
| 10 | |
| 11 | struct bytecode_symbol_iterator { |
| 12 | /* No ownership of bytecode is taken. */ |
| 13 | char *bytecode; |
| 14 | size_t offset, len; |
| 15 | }; |
| 16 | |
| 17 | LTTNG_HIDDEN |
| 18 | struct bytecode_symbol_iterator *bytecode_symbol_iterator_create( |
| 19 | struct lttng_filter_bytecode *bytecode) |
| 20 | { |
| 21 | struct bytecode_symbol_iterator *it = NULL; |
| 22 | |
| 23 | if (!bytecode) { |
| 24 | goto end; |
| 25 | } |
| 26 | |
| 27 | it = zmalloc(sizeof(*it)); |
| 28 | if (!it) { |
| 29 | goto end; |
| 30 | } |
| 31 | |
| 32 | it->bytecode = bytecode->data; |
| 33 | it->offset = bytecode->reloc_table_offset; |
| 34 | it->len = bytecode->len; |
| 35 | end: |
| 36 | return it; |
| 37 | } |
| 38 | |
| 39 | LTTNG_HIDDEN |
| 40 | int bytecode_symbol_iterator_next(struct bytecode_symbol_iterator *it) |
| 41 | { |
| 42 | int ret; |
| 43 | size_t len; |
| 44 | |
| 45 | if (!it || it->offset >= it->len) { |
| 46 | ret = -1; |
| 47 | goto end; |
| 48 | } |
| 49 | |
| 50 | len = strlen(it->bytecode + it->offset + sizeof(uint16_t)) + 1; |
| 51 | it->offset += len + sizeof(uint16_t); |
| 52 | ret = it->offset >= it->len ? -1 : 0; |
| 53 | end: |
| 54 | return ret; |
| 55 | } |
| 56 | |
| 57 | LTTNG_HIDDEN |
| 58 | int bytecode_symbol_iterator_get_type(struct bytecode_symbol_iterator *it) |
| 59 | { |
| 60 | int ret; |
| 61 | |
| 62 | if (!it) { |
| 63 | ret = -1; |
| 64 | goto end; |
| 65 | } |
| 66 | |
| 67 | ret = *((uint16_t *) (it->bytecode + it->offset)); |
| 68 | end: |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | LTTNG_HIDDEN |
| 73 | const char *bytecode_symbol_iterator_get_name( |
| 74 | struct bytecode_symbol_iterator *it) |
| 75 | { |
| 76 | const char *ret = NULL; |
| 77 | |
| 78 | if (!it) { |
| 79 | goto end; |
| 80 | } |
| 81 | |
| 82 | ret = it->bytecode + it->offset + sizeof(uint16_t); |
| 83 | end: |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | LTTNG_HIDDEN |
| 88 | void bytecode_symbol_iterator_destroy(struct bytecode_symbol_iterator *it) |
| 89 | { |
| 90 | free(it); |
| 91 | } |