actions: improve logging in lttng_action_create_from_buffer
[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 <lttng/action/action-internal.h>
9 #include <lttng/action/notify-internal.h>
10 #include <common/error.h>
11 #include <assert.h>
12
13 static const char *lttng_action_type_string(enum lttng_action_type action_type)
14 {
15 switch (action_type) {
16 case LTTNG_ACTION_TYPE_UNKNOWN:
17 return "UNKNOWN";
18 case LTTNG_ACTION_TYPE_NOTIFY:
19 return "NOTIFY";
20 default:
21 return "???";
22 }
23 }
24
25 enum lttng_action_type lttng_action_get_type(struct lttng_action *action)
26 {
27 return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN;
28 }
29
30 LTTNG_HIDDEN
31 enum lttng_action_type lttng_action_get_type_const(
32 const struct lttng_action *action)
33 {
34 return action->type;
35 }
36
37 LTTNG_HIDDEN
38 void lttng_action_init(
39 struct lttng_action *action,
40 enum lttng_action_type type,
41 action_validate_cb validate,
42 action_serialize_cb serialize,
43 action_destroy_cb destroy)
44 {
45 action->type = type;
46 action->validate = validate;
47 action->serialize = serialize;
48 action->destroy = destroy;
49 }
50
51 void lttng_action_destroy(struct lttng_action *action)
52 {
53 if (!action) {
54 return;
55 }
56
57 assert(action->destroy);
58 action->destroy(action);
59 }
60
61 LTTNG_HIDDEN
62 bool lttng_action_validate(struct lttng_action *action)
63 {
64 bool valid;
65
66 if (!action) {
67 valid = false;
68 goto end;
69 }
70
71 if (!action->validate) {
72 /* Sub-class guarantees that it can never be invalid. */
73 valid = true;
74 goto end;
75 }
76
77 valid = action->validate(action);
78 end:
79 return valid;
80 }
81
82 LTTNG_HIDDEN
83 int lttng_action_serialize(struct lttng_action *action,
84 struct lttng_dynamic_buffer *buf)
85 {
86 int ret;
87 struct lttng_action_comm action_comm = {
88 .action_type = (int8_t) action->type,
89 };
90
91 ret = lttng_dynamic_buffer_append(buf, &action_comm,
92 sizeof(action_comm));
93 if (ret) {
94 goto end;
95 }
96
97 ret = action->serialize(action, buf);
98 if (ret) {
99 goto end;
100 }
101 end:
102 return ret;
103 }
104
105 LTTNG_HIDDEN
106 ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view,
107 struct lttng_action **action)
108 {
109 ssize_t consumed_len, specific_action_consumed_len;
110 const struct lttng_action_comm *action_comm;
111 action_create_from_buffer_cb create_from_buffer_cb;
112 struct lttng_buffer_view specific_action_view;
113
114 if (!view || !action) {
115 consumed_len = -1;
116 goto end;
117 }
118
119 action_comm = (const struct lttng_action_comm *) view->data;
120
121 DBG("Create action from buffer: action-type=%s",
122 lttng_action_type_string(action_comm->action_type));
123
124 switch (action_comm->action_type) {
125 case LTTNG_ACTION_TYPE_NOTIFY:
126 create_from_buffer_cb = lttng_action_notify_create_from_buffer;
127 break;
128 default:
129 ERR("Failed to create action from buffer, unhandled action type: action-type=%u (%s)",
130 action_comm->action_type,
131 lttng_action_type_string(
132 action_comm->action_type));
133 consumed_len = -1;
134 goto end;
135 }
136
137 /* Create buffer view for the action-type-specific data. */
138 specific_action_view = lttng_buffer_view_from_view(view,
139 sizeof(struct lttng_action_comm),
140 view->size - sizeof(struct lttng_action_comm));
141
142 specific_action_consumed_len =
143 create_from_buffer_cb(&specific_action_view, action);
144 if (specific_action_consumed_len < 0) {
145 ERR("Failed to create specific action from buffer.");
146 consumed_len = -1;
147 goto end;
148 }
149
150 assert(*action);
151
152 consumed_len = sizeof(struct lttng_action_comm) +
153 specific_action_consumed_len;
154
155 end:
156 return consumed_len;
157 }
This page took 0.032657 seconds and 5 git commands to generate.