| 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 | struct bytecode_symbol_iterator *bytecode_symbol_iterator_create( |
| 18 | struct lttng_bytecode *bytecode) |
| 19 | { |
| 20 | struct bytecode_symbol_iterator *it = NULL; |
| 21 | |
| 22 | if (!bytecode) { |
| 23 | goto end; |
| 24 | } |
| 25 | |
| 26 | it = (bytecode_symbol_iterator *) zmalloc(sizeof(*it)); |
| 27 | if (!it) { |
| 28 | goto end; |
| 29 | } |
| 30 | |
| 31 | it->bytecode = bytecode->data; |
| 32 | it->offset = bytecode->reloc_table_offset; |
| 33 | it->len = bytecode->len; |
| 34 | end: |
| 35 | return it; |
| 36 | } |
| 37 | |
| 38 | int bytecode_symbol_iterator_next(struct bytecode_symbol_iterator *it) |
| 39 | { |
| 40 | int ret; |
| 41 | size_t len; |
| 42 | |
| 43 | if (!it || it->offset >= it->len) { |
| 44 | ret = -1; |
| 45 | goto end; |
| 46 | } |
| 47 | |
| 48 | len = strlen(it->bytecode + it->offset + sizeof(uint16_t)) + 1; |
| 49 | it->offset += len + sizeof(uint16_t); |
| 50 | ret = it->offset >= it->len ? -1 : 0; |
| 51 | end: |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | int bytecode_symbol_iterator_get_type(struct bytecode_symbol_iterator *it) |
| 56 | { |
| 57 | int ret; |
| 58 | |
| 59 | if (!it) { |
| 60 | ret = -1; |
| 61 | goto end; |
| 62 | } |
| 63 | |
| 64 | ret = *((uint16_t *) (it->bytecode + it->offset)); |
| 65 | end: |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | const char *bytecode_symbol_iterator_get_name( |
| 70 | struct bytecode_symbol_iterator *it) |
| 71 | { |
| 72 | const char *ret = NULL; |
| 73 | |
| 74 | if (!it) { |
| 75 | goto end; |
| 76 | } |
| 77 | |
| 78 | ret = it->bytecode + it->offset + sizeof(uint16_t); |
| 79 | end: |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | void bytecode_symbol_iterator_destroy(struct bytecode_symbol_iterator *it) |
| 84 | { |
| 85 | free(it); |
| 86 | } |