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