SoW-2020-0002: Trace Hit Counters
[lttng-tools.git] / src / common / actions / action.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <assert.h>
9 #include <common/error.h>
10 #include <lttng/action/action-internal.h>
11 #include <lttng/action/group-internal.h>
12 #include <lttng/action/incr-value-internal.h>
13 #include <lttng/action/notify-internal.h>
14 #include <lttng/action/rotate-session-internal.h>
15 #include <lttng/action/snapshot-session-internal.h>
16 #include <lttng/action/start-session-internal.h>
17 #include <lttng/action/stop-session-internal.h>
18
19 LTTNG_HIDDEN
20 const char *lttng_action_type_string(enum lttng_action_type action_type)
21 {
22 switch (action_type) {
23 case LTTNG_ACTION_TYPE_UNKNOWN:
24 return "UNKNOWN";
25 case LTTNG_ACTION_TYPE_GROUP:
26 return "GROUP";
27 case LTTNG_ACTION_TYPE_NOTIFY:
28 return "NOTIFY";
29 case LTTNG_ACTION_TYPE_ROTATE_SESSION:
30 return "ROTATE_SESSION";
31 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
32 return "SNAPSHOT_SESSION";
33 case LTTNG_ACTION_TYPE_START_SESSION:
34 return "START_SESSION";
35 case LTTNG_ACTION_TYPE_STOP_SESSION:
36 return "STOP_SESSION";
37 case LTTNG_ACTION_TYPE_INCREMENT_VALUE:
38 return "INCREMENT_VALUE";
39 default:
40 return "???";
41 }
42 }
43
44 enum lttng_action_type lttng_action_get_type(const struct lttng_action *action)
45 {
46 return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN;
47 }
48
49 LTTNG_HIDDEN
50 void lttng_action_init(
51 struct lttng_action *action,
52 enum lttng_action_type type,
53 action_validate_cb validate,
54 action_serialize_cb serialize,
55 action_equal_cb equal,
56 action_destroy_cb destroy)
57 {
58 urcu_ref_init(&action->ref);
59 action->type = type;
60 action->validate = validate;
61 action->serialize = serialize;
62 action->equal = equal;
63 action->destroy = destroy;
64 }
65
66 static
67 void action_destroy_ref(struct urcu_ref *ref)
68 {
69 struct lttng_action *action =
70 container_of(ref, struct lttng_action, ref);
71
72 action->destroy(action);
73 }
74
75 LTTNG_HIDDEN
76 void lttng_action_get(struct lttng_action *action)
77 {
78 urcu_ref_get(&action->ref);
79 }
80
81 LTTNG_HIDDEN
82 void lttng_action_put(struct lttng_action *action)
83 {
84 if (!action) {
85 return;
86 }
87
88 assert(action->destroy);
89 urcu_ref_put(&action->ref, action_destroy_ref);
90 }
91
92 void lttng_action_destroy(struct lttng_action *action)
93 {
94 lttng_action_put(action);
95 }
96
97 LTTNG_HIDDEN
98 bool lttng_action_validate(struct lttng_action *action)
99 {
100 bool valid;
101
102 if (!action) {
103 valid = false;
104 goto end;
105 }
106
107 if (!action->validate) {
108 /* Sub-class guarantees that it can never be invalid. */
109 valid = true;
110 goto end;
111 }
112
113 valid = action->validate(action);
114 end:
115 return valid;
116 }
117
118 LTTNG_HIDDEN
119 int lttng_action_serialize(struct lttng_action *action,
120 struct lttng_payload *payload)
121 {
122 int ret;
123 struct lttng_action_comm action_comm = {
124 .action_type = (int8_t) action->type,
125 };
126
127 ret = lttng_dynamic_buffer_append(&payload->buffer, &action_comm,
128 sizeof(action_comm));
129 if (ret) {
130 goto end;
131 }
132
133 ret = action->serialize(action, payload);
134 if (ret) {
135 goto end;
136 }
137 end:
138 return ret;
139 }
140
141 LTTNG_HIDDEN
142 ssize_t lttng_action_create_from_payload(struct lttng_payload_view *view,
143 struct lttng_action **action)
144 {
145 ssize_t consumed_len, specific_action_consumed_len;
146 action_create_from_payload_cb create_from_payload_cb;
147 const struct lttng_action_comm *action_comm;
148 const struct lttng_payload_view action_comm_view =
149 lttng_payload_view_from_view(
150 view, 0, sizeof(*action_comm));
151
152 if (!view || !action) {
153 consumed_len = -1;
154 goto end;
155 }
156
157 if (!lttng_payload_view_is_valid(&action_comm_view)) {
158 /* Payload not large enough to contain the header. */
159 consumed_len = -1;
160 goto end;
161 }
162
163 action_comm = (const struct lttng_action_comm *) action_comm_view.buffer.data;
164
165 DBG("Create action from payload: action-type=%s",
166 lttng_action_type_string(action_comm->action_type));
167
168 switch (action_comm->action_type) {
169 case LTTNG_ACTION_TYPE_NOTIFY:
170 create_from_payload_cb = lttng_action_notify_create_from_payload;
171 break;
172 case LTTNG_ACTION_TYPE_ROTATE_SESSION:
173 create_from_payload_cb =
174 lttng_action_rotate_session_create_from_payload;
175 break;
176 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
177 create_from_payload_cb =
178 lttng_action_snapshot_session_create_from_payload;
179 break;
180 case LTTNG_ACTION_TYPE_START_SESSION:
181 create_from_payload_cb =
182 lttng_action_start_session_create_from_payload;
183 break;
184 case LTTNG_ACTION_TYPE_STOP_SESSION:
185 create_from_payload_cb =
186 lttng_action_stop_session_create_from_payload;
187 break;
188 case LTTNG_ACTION_TYPE_INCREMENT_VALUE:
189 create_from_payload_cb =
190 lttng_action_incr_value_create_from_payload;
191 break;
192 case LTTNG_ACTION_TYPE_GROUP:
193 create_from_payload_cb = lttng_action_group_create_from_payload;
194 break;
195 default:
196 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
197 action_comm->action_type,
198 lttng_action_type_string(
199 action_comm->action_type));
200 consumed_len = -1;
201 goto end;
202 }
203
204 {
205 /* Create buffer view for the action-type-specific data. */
206 struct lttng_payload_view specific_action_view =
207 lttng_payload_view_from_view(view,
208 sizeof(struct lttng_action_comm),
209 -1);
210
211 specific_action_consumed_len = create_from_payload_cb(
212 &specific_action_view, action);
213 }
214 if (specific_action_consumed_len < 0) {
215 ERR("Failed to create specific action from buffer.");
216 consumed_len = -1;
217 goto end;
218 }
219
220 assert(*action);
221
222 consumed_len = sizeof(struct lttng_action_comm) +
223 specific_action_consumed_len;
224
225 end:
226 return consumed_len;
227 }
228
229 LTTNG_HIDDEN
230 bool lttng_action_is_equal(const struct lttng_action *a,
231 const struct lttng_action *b)
232 {
233 bool is_equal = false;
234
235 if (!a || !b) {
236 goto end;
237 }
238
239 if (a->type != b->type) {
240 goto end;
241 }
242
243 if (a == b) {
244 is_equal = true;
245 goto end;
246 }
247
248 assert(a->equal);
249 is_equal = a->equal(a, b);
250 end:
251 return is_equal;
252 }
This page took 0.037987 seconds and 5 git commands to generate.