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