SoW-2020-0003: Trace Hit Counters
[lttng-tools.git] / src / common / actions / action.c
CommitLineData
a58c490f 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
a58c490f 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
a58c490f 5 *
a58c490f
JG
6 */
7
14ec7e87
JR
8#include <assert.h>
9#include <common/error.h>
a58c490f 10#include <lttng/action/action-internal.h>
0c51e8f3 11#include <lttng/action/group-internal.h>
ebdb334b 12#include <lttng/action/incr-value-internal.h>
a58c490f 13#include <lttng/action/notify-internal.h>
bfb2ec6a 14#include <lttng/action/rotate-session-internal.h>
757c48a2 15#include <lttng/action/snapshot-session-internal.h>
58397d0d 16#include <lttng/action/start-session-internal.h>
931bdbaa 17#include <lttng/action/stop-session-internal.h>
a58c490f 18
10615eee
JR
19LTTNG_HIDDEN
20const char *lttng_action_type_string(enum lttng_action_type action_type)
2666d352
SM
21{
22 switch (action_type) {
23 case LTTNG_ACTION_TYPE_UNKNOWN:
24 return "UNKNOWN";
0c51e8f3
SM
25 case LTTNG_ACTION_TYPE_GROUP:
26 return "GROUP";
2666d352
SM
27 case LTTNG_ACTION_TYPE_NOTIFY:
28 return "NOTIFY";
bfb2ec6a
SM
29 case LTTNG_ACTION_TYPE_ROTATE_SESSION:
30 return "ROTATE_SESSION";
757c48a2
SM
31 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
32 return "SNAPSHOT_SESSION";
58397d0d
SM
33 case LTTNG_ACTION_TYPE_START_SESSION:
34 return "START_SESSION";
931bdbaa
SM
35 case LTTNG_ACTION_TYPE_STOP_SESSION:
36 return "STOP_SESSION";
ebdb334b
JR
37 case LTTNG_ACTION_TYPE_INCREMENT_VALUE:
38 return "INCREMENT_VALUE";
2666d352
SM
39 default:
40 return "???";
41 }
42}
43
17182cfd 44enum lttng_action_type lttng_action_get_type(const struct lttng_action *action)
a58c490f
JG
45{
46 return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN;
47}
48
6acb3f46
SM
49LTTNG_HIDDEN
50void lttng_action_init(
51 struct lttng_action *action,
52 enum lttng_action_type type,
53 action_validate_cb validate,
54 action_serialize_cb serialize,
3dd04a6a 55 action_equal_cb equal,
6acb3f46
SM
56 action_destroy_cb destroy)
57{
c852ce4e 58 urcu_ref_init(&action->ref);
6acb3f46
SM
59 action->type = type;
60 action->validate = validate;
61 action->serialize = serialize;
3dd04a6a 62 action->equal = equal;
6acb3f46
SM
63 action->destroy = destroy;
64}
65
c852ce4e
JG
66static
67void 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
75LTTNG_HIDDEN
76void lttng_action_get(struct lttng_action *action)
77{
78 urcu_ref_get(&action->ref);
79}
80
81LTTNG_HIDDEN
82void lttng_action_put(struct lttng_action *action)
a58c490f
JG
83{
84 if (!action) {
85 return;
86 }
87
88 assert(action->destroy);
c852ce4e
JG
89 urcu_ref_put(&action->ref, action_destroy_ref);
90}
91
92void lttng_action_destroy(struct lttng_action *action)
93{
94 lttng_action_put(action);
a58c490f
JG
95}
96
97LTTNG_HIDDEN
98bool 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);
114end:
115 return valid;
116}
117
118LTTNG_HIDDEN
3647288f 119int lttng_action_serialize(struct lttng_action *action,
c0a66c84 120 struct lttng_payload *payload)
a58c490f 121{
3647288f
JG
122 int ret;
123 struct lttng_action_comm action_comm = {
124 .action_type = (int8_t) action->type,
125 };
126
c0a66c84 127 ret = lttng_dynamic_buffer_append(&payload->buffer, &action_comm,
3647288f
JG
128 sizeof(action_comm));
129 if (ret) {
a58c490f
JG
130 goto end;
131 }
132
c0a66c84 133 ret = action->serialize(action, payload);
3647288f 134 if (ret) {
a58c490f
JG
135 goto end;
136 }
a58c490f
JG
137end:
138 return ret;
139}
140
141LTTNG_HIDDEN
c0a66c84 142ssize_t lttng_action_create_from_payload(struct lttng_payload_view *view,
869a3c2d 143 struct lttng_action **action)
a58c490f 144{
869a3c2d 145 ssize_t consumed_len, specific_action_consumed_len;
c0a66c84 146 action_create_from_payload_cb create_from_payload_cb;
3e6e0df2
JG
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));
a58c490f 151
869a3c2d
SM
152 if (!view || !action) {
153 consumed_len = -1;
a58c490f
JG
154 goto end;
155 }
156
3e6e0df2
JG
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;
869a3c2d 164
c0a66c84 165 DBG("Create action from payload: action-type=%s",
2666d352
SM
166 lttng_action_type_string(action_comm->action_type));
167
a58c490f
JG
168 switch (action_comm->action_type) {
169 case LTTNG_ACTION_TYPE_NOTIFY:
c0a66c84 170 create_from_payload_cb = lttng_action_notify_create_from_payload;
a58c490f 171 break;
bfb2ec6a 172 case LTTNG_ACTION_TYPE_ROTATE_SESSION:
c0a66c84
JG
173 create_from_payload_cb =
174 lttng_action_rotate_session_create_from_payload;
bfb2ec6a 175 break;
757c48a2
SM
176 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
177 create_from_payload_cb =
178 lttng_action_snapshot_session_create_from_payload;
179 break;
58397d0d 180 case LTTNG_ACTION_TYPE_START_SESSION:
c0a66c84
JG
181 create_from_payload_cb =
182 lttng_action_start_session_create_from_payload;
58397d0d 183 break;
931bdbaa 184 case LTTNG_ACTION_TYPE_STOP_SESSION:
c0a66c84
JG
185 create_from_payload_cb =
186 lttng_action_stop_session_create_from_payload;
931bdbaa 187 break;
ebdb334b
JR
188 case LTTNG_ACTION_TYPE_INCREMENT_VALUE:
189 create_from_payload_cb =
190 lttng_action_incr_value_create_from_payload;
191 break;
0c51e8f3
SM
192 case LTTNG_ACTION_TYPE_GROUP:
193 create_from_payload_cb = lttng_action_group_create_from_payload;
194 break;
a58c490f 195 default:
c0a66c84 196 ERR("Failed to create action from payload, unhandled action type: action-type=%u (%s)",
2666d352
SM
197 action_comm->action_type,
198 lttng_action_type_string(
199 action_comm->action_type));
869a3c2d 200 consumed_len = -1;
a58c490f
JG
201 goto end;
202 }
203
c0a66c84
JG
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);
869a3c2d 210
c0a66c84
JG
211 specific_action_consumed_len = create_from_payload_cb(
212 &specific_action_view, action);
213 }
869a3c2d
SM
214 if (specific_action_consumed_len < 0) {
215 ERR("Failed to create specific action from buffer.");
216 consumed_len = -1;
a58c490f
JG
217 goto end;
218 }
869a3c2d
SM
219
220 assert(*action);
221
222 consumed_len = sizeof(struct lttng_action_comm) +
223 specific_action_consumed_len;
224
a58c490f 225end:
869a3c2d 226 return consumed_len;
a58c490f 227}
3dd04a6a
JR
228
229LTTNG_HIDDEN
230bool 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);
250end:
251 return is_equal;
252}
This page took 0.0563 seconds and 5 git commands to generate.