Commit | Line | Data |
---|---|---|
71a559f8 JG |
1 | /* |
2 | * filter.c | |
3 | * | |
4 | * LTTng filter bytecode utilities. | |
5 | * | |
6 | * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
10 | * as published by the Free Software Foundation. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public License | |
18 | * along with this library; if not, write to the Free Software Foundation, | |
19 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include "filter.h" | |
23 | #include <stddef.h> | |
24 | ||
25 | struct bytecode_symbol_iterator { | |
26 | /* No ownership of bytecode is taken. */ | |
27 | char *bytecode; | |
28 | size_t offset, len; | |
29 | }; | |
30 | ||
31 | struct bytecode_symbol_iterator *bytecode_symbol_iterator_create( | |
32 | struct lttng_filter_bytecode *bytecode) | |
33 | { | |
34 | struct bytecode_symbol_iterator *it = NULL; | |
35 | ||
36 | if (!bytecode) { | |
37 | goto end; | |
38 | } | |
39 | ||
40 | it = zmalloc(sizeof(*it)); | |
41 | if (!it) { | |
42 | goto end; | |
43 | } | |
44 | ||
45 | it->bytecode = bytecode->data; | |
46 | it->offset = bytecode->reloc_table_offset; | |
47 | it->len = bytecode->len; | |
48 | end: | |
49 | return it; | |
50 | } | |
51 | ||
52 | int bytecode_symbol_iterator_next(struct bytecode_symbol_iterator *it) | |
53 | { | |
54 | int ret; | |
55 | size_t len; | |
56 | ||
57 | if (!it || it->offset >= it->len) { | |
58 | ret = -1; | |
59 | goto end; | |
60 | } | |
61 | ||
62 | len = strlen(it->bytecode + it->offset + sizeof(uint16_t)) + 1; | |
63 | it->offset += len + sizeof(uint16_t); | |
64 | ret = it->offset >= it->len ? -1 : 0; | |
65 | end: | |
66 | return ret; | |
67 | } | |
68 | ||
69 | int bytecode_symbol_iterator_get_type(struct bytecode_symbol_iterator *it) | |
70 | { | |
71 | int ret; | |
72 | ||
73 | if (!it) { | |
74 | ret = -1; | |
75 | goto end; | |
76 | } | |
77 | ||
78 | ret = *((uint16_t *) (it->bytecode + it->offset)); | |
79 | end: | |
80 | return ret; | |
81 | } | |
82 | ||
83 | const char *bytecode_symbol_iterator_get_name( | |
84 | struct bytecode_symbol_iterator *it) | |
85 | { | |
86 | const char *ret = NULL; | |
87 | ||
88 | if (!it) { | |
89 | goto end; | |
90 | } | |
91 | ||
92 | ret = it->bytecode + it->offset + sizeof(uint16_t); | |
93 | end: | |
94 | return ret; | |
95 | } | |
96 | ||
97 | void bytecode_symbol_iterator_destroy(struct bytecode_symbol_iterator *it) | |
98 | { | |
99 | free(it); | |
100 | } |