event-rule: lttng_event_rule base object
[lttng-tools.git] / src / common / event-rule / event-rule.c
CommitLineData
7a3dcaf6
JR
1/*
2 * Copyright (C) 2019 Jonathan Rajotte
3 * <jonathan.rajotte-julien@efficios.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only
6 *
7 */
8
9#include <assert.h>
10#include <common/error.h>
11#include <common/macros.h>
12#include <common/payload.h>
13#include <common/payload-view.h>
14#include <lttng/event-rule/event-rule-internal.h>
15#include <stdbool.h>
16
17enum lttng_event_rule_type lttng_event_rule_get_type(
18 const struct lttng_event_rule *event_rule)
19{
20 return event_rule ? event_rule->type : LTTNG_EVENT_RULE_TYPE_UNKNOWN;
21}
22
23LTTNG_HIDDEN
24enum lttng_domain_type lttng_event_rule_get_domain_type(
25 const struct lttng_event_rule *event_rule)
26{
27 enum lttng_domain_type domain_type = LTTNG_DOMAIN_NONE;
28
29 switch (lttng_event_rule_get_type(event_rule)) {
30 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
31 /* TODO */
32 domain_type = LTTNG_DOMAIN_NONE;
33 break;
34 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
35 case LTTNG_EVENT_RULE_TYPE_KPROBE:
36 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
37 case LTTNG_EVENT_RULE_TYPE_UPROBE:
38 domain_type = LTTNG_DOMAIN_KERNEL;
39 break;
40 case LTTNG_EVENT_RULE_TYPE_UNKNOWN:
41 domain_type = LTTNG_DOMAIN_NONE;
42 break;
43 }
44
45 return domain_type;
46}
47
48static void lttng_event_rule_release(struct urcu_ref *ref)
49{
50 struct lttng_event_rule *event_rule =
51 container_of(ref, typeof(*event_rule), ref);
52
53 assert(event_rule->destroy);
54 event_rule->destroy(event_rule);
55}
56
57void lttng_event_rule_destroy(struct lttng_event_rule *event_rule)
58{
59 lttng_event_rule_put(event_rule);
60}
61
62LTTNG_HIDDEN
63bool lttng_event_rule_validate(const struct lttng_event_rule *event_rule)
64{
65 bool valid;
66
67 if (!event_rule) {
68 valid = false;
69 goto end;
70 }
71
72 if (!event_rule->validate) {
73 /* Sub-class guarantees that it can never be invalid. */
74 valid = true;
75 goto end;
76 }
77
78 valid = event_rule->validate(event_rule);
79end:
80 return valid;
81}
82
83LTTNG_HIDDEN
84int lttng_event_rule_serialize(const struct lttng_event_rule *event_rule,
85 struct lttng_payload *payload)
86{
87 int ret;
88 struct lttng_event_rule_comm event_rule_comm = {};
89
90 if (!event_rule) {
91 ret = -1;
92 goto end;
93 }
94
95 event_rule_comm.event_rule_type = (int8_t) event_rule->type;
96
97 ret = lttng_dynamic_buffer_append(
98 &payload->buffer, &event_rule_comm, sizeof(event_rule_comm));
99 if (ret) {
100 goto end;
101 }
102
103 ret = event_rule->serialize(event_rule, payload);
104 if (ret) {
105 goto end;
106 }
107end:
108 return ret;
109}
110
111LTTNG_HIDDEN
112bool lttng_event_rule_is_equal(const struct lttng_event_rule *a,
113 const struct lttng_event_rule *b)
114{
115 bool is_equal = false;
116
117 if (!a || !b) {
118 goto end;
119 }
120
121 if (a->type != b->type) {
122 goto end;
123 }
124
125 if (a == b) {
126 is_equal = true;
127 goto end;
128 }
129
130 is_equal = a->equal ? a->equal(a, b) : true;
131end:
132 return is_equal;
133}
134
135LTTNG_HIDDEN
136ssize_t lttng_event_rule_create_from_payload(
137 struct lttng_payload_view *view,
138 struct lttng_event_rule **event_rule)
139{
140 ssize_t ret, consumed = 0;
141 const struct lttng_event_rule_comm *event_rule_comm;
142 event_rule_create_from_payload_cb create_from_payload = NULL;
143
144 if (!view || !event_rule) {
145 ret = -1;
146 goto end;
147 }
148
149 DBG("Deserializing event_rule from payload");
150 event_rule_comm = (const struct lttng_event_rule_comm *) view->buffer.data;
151 consumed += sizeof(*event_rule_comm);
152
153 switch ((enum lttng_event_rule_type) event_rule_comm->event_rule_type) {
154 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
155 /* TODO */
156 break;
157 case LTTNG_EVENT_RULE_TYPE_KPROBE:
158 /* TODO */
159 break;
160 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
161 /* TODO */
162 break;
163 case LTTNG_EVENT_RULE_TYPE_UPROBE:
164 /* TODO */
165 break;
166 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
167 /* TODO */
168 break;
169 default:
170 ERR("Attempted to create event rule of unknown type (%i)",
171 (int) event_rule_comm->event_rule_type);
172 ret = -1;
173 goto end;
174 }
175
176 assert(create_from_payload);
177
178 {
179 struct lttng_payload_view child_view =
180 lttng_payload_view_from_view(
181 view, consumed, -1);
182
183 ret = create_from_payload(&child_view, event_rule);
184 if (ret < 0) {
185 goto end;
186 }
187
188 consumed += ret;
189 }
190
191 if (!lttng_event_rule_validate(*event_rule)) {
192 ret = -1;
193 goto end;
194 }
195
196 ret = consumed;
197end:
198 return ret;
199}
200
201LTTNG_HIDDEN
202void lttng_event_rule_init(struct lttng_event_rule *event_rule,
203 enum lttng_event_rule_type type)
204{
205 urcu_ref_init(&event_rule->ref);
206 event_rule->type = type;
207}
208
209LTTNG_HIDDEN
210bool lttng_event_rule_get(struct lttng_event_rule *event_rule)
211{
212 return urcu_ref_get_unless_zero(&event_rule->ref);
213}
214
215LTTNG_HIDDEN
216void lttng_event_rule_put(struct lttng_event_rule *event_rule)
217{
218 if (!event_rule) {
219 return;
220 }
221
222 assert(event_rule->ref.refcount);
223 urcu_ref_put(&event_rule->ref, lttng_event_rule_release);
224}
225
226LTTNG_HIDDEN
227enum lttng_error_code lttng_event_rule_generate_filter_bytecode(
228 struct lttng_event_rule *rule, uid_t uid, gid_t gid)
229{
230 assert(rule->generate_filter_bytecode);
231 return rule->generate_filter_bytecode(rule, uid, gid);
232}
233
234LTTNG_HIDDEN
235const char *lttng_event_rule_get_filter(const struct lttng_event_rule *rule)
236{
237 assert(rule->get_filter);
238 return rule->get_filter(rule);
239}
240
241LTTNG_HIDDEN
242const struct lttng_filter_bytecode *lttng_event_rule_get_filter_bytecode(
243 const struct lttng_event_rule *rule)
244{
245 assert(rule->get_filter_bytecode);
246 return rule->get_filter_bytecode(rule);
247}
248
249LTTNG_HIDDEN
250struct lttng_event_exclusion *lttng_event_rule_generate_exclusions(
251 const struct lttng_event_rule *rule)
252{
253 assert(rule->generate_exclusions);
254 return rule->generate_exclusions(rule);
255}
256
257LTTNG_HIDDEN
258const char *lttng_event_rule_type_str(enum lttng_event_rule_type type)
259{
260 switch (type) {
261 case LTTNG_EVENT_RULE_TYPE_UNKNOWN:
262 return "unknown";
263 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
264 return "tracepoint";
265 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
266 return "syscall";
267 case LTTNG_EVENT_RULE_TYPE_KPROBE:
268 return "probe";
269 case LTTNG_EVENT_RULE_TYPE_KRETPROBE:
270 return "function";
271 case LTTNG_EVENT_RULE_TYPE_UPROBE:
272 return "userspace-probe";
273 default:
274 abort();
275 }
276}
This page took 0.048323 seconds and 4 git commands to generate.