Common Trace Format 2 generation
[lttng-tools.git] / src / common / tracker.cpp
1 /*
2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
3 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only
6 *
7 */
8
9 #include <lttng/domain.h>
10 #include <lttng/lttng-error.h>
11 #include <lttng/tracker.h>
12
13 #include <common/dynamic-array.hpp>
14 #include <common/error.hpp>
15 #include <common/hashtable/hashtable.hpp>
16 #include <common/hashtable/utils.hpp>
17 #include <common/tracker.hpp>
18
19 #include <stdbool.h>
20 #include <type_traits>
21
22 struct process_attr_tracker_values_comm_header {
23 uint32_t count;
24 };
25
26 struct process_attr_tracker_value_comm {
27 /* enum lttng_process_attr_value_type */
28 int32_t type;
29 union {
30 struct process_attr_integral_value_comm integral;
31 /* Includes the '\0' terminator. */
32 uint32_t name_len;
33 } value;
34 };
35
36 #define GET_INTEGRAL_COMM_VALUE(value_ptr, as_type) \
37 ((as_type)(std::is_signed<as_type>::value ? \
38 (value_ptr)->u._signed : (value_ptr)->u._unsigned))
39
40 #define SET_INTEGRAL_COMM_VALUE(comm_value, from_value) \
41 if (std::is_signed<decltype(from_value)>::value) { \
42 (comm_value)->u._signed = \
43 (typeof((comm_value)->u._signed)) from_value; \
44 } else { \
45 (comm_value)->u._unsigned = \
46 (typeof((comm_value)->u._unsigned)) from_value; \
47 }
48
49 static inline bool is_virtual_process_attr(enum lttng_process_attr process_attr)
50 {
51 return process_attr == LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID ||
52 process_attr == LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID ||
53 process_attr == LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID;
54 }
55
56 static inline bool is_value_type_name(
57 enum lttng_process_attr_value_type value_type)
58 {
59 return value_type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME ||
60 value_type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME;
61 }
62
63 enum lttng_error_code process_attr_value_from_comm(
64 enum lttng_domain_type domain,
65 enum lttng_process_attr process_attr,
66 enum lttng_process_attr_value_type value_type,
67 const struct process_attr_integral_value_comm *integral_value,
68 const struct lttng_buffer_view *value_view,
69 struct process_attr_value **_value)
70 {
71 char *name = NULL;
72 enum lttng_error_code ret = LTTNG_OK;
73 struct process_attr_value *value = (process_attr_value *) zmalloc(sizeof(*value));
74
75 if (!value) {
76 ret = LTTNG_ERR_NOMEM;
77 goto error;
78 }
79
80 if (value_view && value_view->size > 0) {
81 if (value_view->data[value_view->size - 1] != '\0') {
82 ret = LTTNG_ERR_INVALID;
83 goto error;
84 }
85 name = strdup(value_view->data);
86 if (!name) {
87 ret = LTTNG_ERR_NOMEM;
88 goto error;
89 }
90 }
91
92 if (domain != LTTNG_DOMAIN_UST && domain != LTTNG_DOMAIN_KERNEL) {
93 ERR("Only the user space and kernel space domains may be specified to configure process attribute trackers");
94 ret = LTTNG_ERR_UNSUPPORTED_DOMAIN;
95 goto error;
96 }
97
98 if (!is_virtual_process_attr(process_attr) &&
99 domain != LTTNG_DOMAIN_KERNEL) {
100 ERR("Non-virtual process attributes can only be used in the kernel domain");
101 ret = LTTNG_ERR_UNSUPPORTED_DOMAIN;
102 goto error;
103 }
104
105 /* Only expect a payload for name value types. */
106 if (is_value_type_name(value_type) &&
107 (!value_view || value_view->size == 0)) {
108 ret = LTTNG_ERR_INVALID_PROTOCOL;
109 goto error;
110 } else if (!is_value_type_name(value_type) && value_view &&
111 value_view->size != 0) {
112 ret = LTTNG_ERR_INVALID_PROTOCOL;
113 goto error;
114 }
115
116 value->type = value_type;
117 switch (process_attr) {
118 case LTTNG_PROCESS_ATTR_PROCESS_ID:
119 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
120 if (value_type != LTTNG_PROCESS_ATTR_VALUE_TYPE_PID) {
121 ERR("Invalid value type used for process ID process attribute");
122 ret = LTTNG_ERR_INVALID;
123 goto error;
124 }
125 value->value.pid =
126 GET_INTEGRAL_COMM_VALUE(integral_value, pid_t);
127 break;
128 case LTTNG_PROCESS_ATTR_USER_ID:
129 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
130 switch (value_type) {
131 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID:
132 value->value.uid = GET_INTEGRAL_COMM_VALUE(
133 integral_value, uid_t);
134 break;
135 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME:
136 if (!name) {
137 ret = LTTNG_ERR_INVALID;
138 goto error;
139 }
140
141 value->value.user_name = name;
142 name = NULL;
143 break;
144 default:
145 ERR("Invalid value type used for user ID process attribute");
146 ret = LTTNG_ERR_INVALID;
147 goto error;
148 }
149 break;
150 case LTTNG_PROCESS_ATTR_GROUP_ID:
151 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
152 switch (value_type) {
153 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID:
154 value->value.gid = GET_INTEGRAL_COMM_VALUE(
155 integral_value, gid_t);
156 break;
157 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME:
158 if (!name) {
159 ret = LTTNG_ERR_INVALID;
160 goto error;
161 }
162
163 value->value.group_name = name;
164 name = NULL;
165 break;
166 default:
167 ERR("Invalid value type used for group ID process attribute");
168 ret = LTTNG_ERR_INVALID;
169 goto error;
170 }
171 break;
172 default:
173 ret = LTTNG_ERR_INVALID_PROTOCOL;
174 goto error;
175 }
176
177 *_value = value;
178 value = NULL;
179 free(name);
180 return LTTNG_OK;
181 error:
182 free(name);
183 process_attr_value_destroy(value);
184 return ret;
185 }
186
187 const char *lttng_process_attr_to_string(enum lttng_process_attr process_attr)
188 {
189 switch (process_attr) {
190 case LTTNG_PROCESS_ATTR_PROCESS_ID:
191 return "process ID";
192 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
193 return "virtual process ID";
194 case LTTNG_PROCESS_ATTR_USER_ID:
195 return "user ID";
196 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
197 return "virtual user ID";
198 case LTTNG_PROCESS_ATTR_GROUP_ID:
199 return "group ID";
200 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
201 return "virtual group ID";
202 default:
203 return "unknown process attribute";
204 }
205 }
206
207 static void process_attr_tracker_value_destructor(void *ptr)
208 {
209 struct process_attr_value *value = (typeof(value)) ptr;
210
211 process_attr_value_destroy(value);
212 }
213
214 struct lttng_process_attr_values *lttng_process_attr_values_create(void)
215 {
216 struct lttng_process_attr_values *values = (lttng_process_attr_values *) zmalloc(sizeof(*values));
217
218 if (!values) {
219 goto end;
220 }
221
222 lttng_dynamic_pointer_array_init(
223 &values->array, process_attr_tracker_value_destructor);
224 end:
225 return values;
226 }
227
228 unsigned int _lttng_process_attr_values_get_count(
229 const struct lttng_process_attr_values *values)
230 {
231 return (unsigned int) lttng_dynamic_pointer_array_get_count(
232 &values->array);
233 }
234
235 const struct process_attr_value *lttng_process_attr_tracker_values_get_at_index(
236 const struct lttng_process_attr_values *values,
237 unsigned int index)
238 {
239 return (process_attr_value *) lttng_dynamic_pointer_array_get_pointer(&values->array, index);
240 }
241
242 static
243 int process_attr_tracker_value_serialize(const struct process_attr_value *value,
244 struct lttng_dynamic_buffer *buffer)
245 {
246 int ret;
247 struct process_attr_tracker_value_comm value_comm = {
248 .type = (int32_t) value->type,
249 };
250 const char *name = NULL;
251
252 switch (value->type) {
253 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID:
254 SET_INTEGRAL_COMM_VALUE(
255 &value_comm.value.integral, value->value.pid);
256 break;
257 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID:
258 SET_INTEGRAL_COMM_VALUE(
259 &value_comm.value.integral, value->value.uid);
260 break;
261 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID:
262 SET_INTEGRAL_COMM_VALUE(
263 &value_comm.value.integral, value->value.gid);
264 break;
265 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME:
266 name = value->value.user_name;
267 break;
268 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME:
269 name = value->value.group_name;
270 break;
271 default:
272 abort();
273 }
274
275 if (name) {
276 value_comm.value.name_len = strlen(name) + 1;
277 }
278
279 ret = lttng_dynamic_buffer_append(
280 buffer, &value_comm, sizeof(value_comm));
281 if (ret) {
282 goto end;
283 }
284
285 if (name) {
286 ret = lttng_dynamic_buffer_append(
287 buffer, name, value_comm.value.name_len);
288 }
289 end:
290 return ret;
291 }
292
293 int lttng_process_attr_values_serialize(
294 const struct lttng_process_attr_values *values,
295 struct lttng_dynamic_buffer *buffer)
296 {
297 int ret;
298 unsigned int count, i;
299 struct process_attr_tracker_values_comm_header header = {};
300
301 count = _lttng_process_attr_values_get_count(values);
302 header.count = (uint32_t) count;
303
304 ret = lttng_dynamic_buffer_append(buffer, &header, sizeof(header));
305 if (ret) {
306 goto end;
307 }
308
309 for (i = 0; i < count; i++) {
310 const struct process_attr_value *value =
311 lttng_process_attr_tracker_values_get_at_index(
312 values, i);
313
314 ret = process_attr_tracker_value_serialize(value, buffer);
315 if (ret) {
316 goto end;
317 }
318 }
319 end:
320 return ret;
321 }
322
323 ssize_t lttng_process_attr_values_create_from_buffer(
324 enum lttng_domain_type domain,
325 enum lttng_process_attr process_attr,
326 const struct lttng_buffer_view *buffer_view,
327 struct lttng_process_attr_values **_values)
328 {
329 ssize_t offset;
330 unsigned int i;
331 struct lttng_process_attr_values *values;
332 struct lttng_buffer_view header_view;
333 const struct process_attr_tracker_values_comm_header *header;
334
335 values = lttng_process_attr_values_create();
336 if (!values) {
337 goto error;
338 }
339
340 header_view = lttng_buffer_view_from_view(
341 buffer_view, 0, sizeof(*header));
342 if (!lttng_buffer_view_is_valid(&header_view)) {
343 goto error;
344 }
345
346 offset = header_view.size;
347 header = (typeof(header)) header_view.data;
348
349 /*
350 * Check that the number of values is not absurdly large with respect to
351 * the received buffer's size.
352 */
353 if (buffer_view->size <
354 header->count * sizeof(struct process_attr_tracker_value_comm)) {
355 goto error;
356 }
357 for (i = 0; i < (unsigned int) header->count; i++) {
358 int ret;
359 enum lttng_error_code ret_code;
360 const struct process_attr_tracker_value_comm *value_comm;
361 struct process_attr_value *value;
362 enum lttng_process_attr_value_type type;
363 struct lttng_buffer_view value_view;
364 struct lttng_buffer_view value_name_view = {};
365
366 value_view = lttng_buffer_view_from_view(
367 buffer_view, offset, sizeof(*value_comm));
368 if (!lttng_buffer_view_is_valid(&value_view)) {
369 goto error;
370 }
371
372 offset += value_view.size;
373 value_comm = (typeof(value_comm)) value_view.data;
374 type = (typeof(type)) value_comm->type;
375
376 if (is_value_type_name(type)) {
377 value_name_view = lttng_buffer_view_from_view(
378 buffer_view, offset,
379 value_comm->value.name_len);
380 if (!lttng_buffer_view_is_valid(&value_name_view)) {
381 goto error;
382 }
383
384 offset += value_name_view.size;
385 }
386
387 ret_code = process_attr_value_from_comm(domain, process_attr,
388 type, &value_comm->value.integral,
389 &value_name_view, &value);
390 if (ret_code != LTTNG_OK) {
391 goto error;
392 }
393
394 ret = lttng_dynamic_pointer_array_add_pointer(
395 &values->array, value);
396 if (ret) {
397 process_attr_value_destroy(value);
398 goto error;
399 }
400 }
401
402 *_values = values;
403 return offset;
404 error:
405 lttng_process_attr_values_destroy(values);
406 return -1;
407 }
408
409 void lttng_process_attr_values_destroy(struct lttng_process_attr_values *values)
410 {
411 if (!values) {
412 return;
413 }
414 lttng_dynamic_pointer_array_reset(&values->array);
415 free(values);
416 }
417
418 struct process_attr_value *process_attr_value_copy(
419 const struct process_attr_value *value)
420 {
421 struct process_attr_value *new_value = NULL;
422
423 if (!value) {
424 goto end;
425 }
426
427 new_value = (process_attr_value *) zmalloc(sizeof(*new_value));
428 if (!new_value) {
429 goto end;
430 }
431 if (is_value_type_name(value->type)) {
432 const char *src =
433 value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME ?
434 value->value.user_name :
435 value->value.group_name;
436 char **dst = value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME ?
437 &new_value->value.user_name :
438 &new_value->value.group_name;
439
440 new_value->type = value->type;
441 *dst = strdup(src);
442 if (!*dst) {
443 goto error;
444 }
445 } else {
446 *new_value = *value;
447 }
448 end:
449 return new_value;
450 error:
451 free(new_value);
452 return NULL;
453 }
454
455 unsigned long process_attr_value_hash(const struct process_attr_value *a)
456 {
457 unsigned long hash = hash_key_ulong((void *) a->type, lttng_ht_seed);
458
459 switch (a->type) {
460 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID:
461 hash ^= hash_key_ulong((void *) (unsigned long) a->value.pid,
462 lttng_ht_seed);
463 break;
464 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID:
465 hash ^= hash_key_ulong((void *) (unsigned long) a->value.uid,
466 lttng_ht_seed);
467 break;
468 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID:
469 hash ^= hash_key_ulong((void *) (unsigned long) a->value.gid,
470 lttng_ht_seed);
471 break;
472 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME:
473 hash ^= hash_key_str(a->value.user_name, lttng_ht_seed);
474 break;
475 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME:
476 hash ^= hash_key_str(a->value.group_name, lttng_ht_seed);
477 break;
478 default:
479 abort();
480 }
481
482 return hash;
483 }
484
485 bool process_attr_tracker_value_equal(const struct process_attr_value *a,
486 const struct process_attr_value *b)
487 {
488 if (a->type != b->type) {
489 return false;
490 }
491 switch (a->type) {
492 case LTTNG_PROCESS_ATTR_VALUE_TYPE_PID:
493 return a->value.pid == b->value.pid;
494 case LTTNG_PROCESS_ATTR_VALUE_TYPE_UID:
495 return a->value.uid == b->value.uid;
496 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GID:
497 return a->value.gid == b->value.gid;
498 case LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME:
499 return !strcmp(a->value.user_name, b->value.user_name);
500 case LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME:
501 return !strcmp(a->value.group_name, b->value.group_name);
502 default:
503 abort();
504 }
505 }
506
507 void process_attr_value_destroy(struct process_attr_value *value)
508 {
509 if (!value) {
510 return;
511 }
512 if (is_value_type_name(value->type)) {
513 free(value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME ?
514 value->value.user_name :
515 value->value.group_name);
516 }
517 free(value);
518 }
This page took 0.039827 seconds and 5 git commands to generate.