1 #ifndef BABELTRACE_COMMON_INTERNAL_H
2 #define BABELTRACE_COMMON_INTERNAL_H
5 * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation
6 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36 #include <babeltrace2/graph/self-message-iterator.h>
37 #include <babeltrace2/trace-ir/event-class-const.h>
38 #include <babeltrace2/trace-ir/field-class-const.h>
39 #include <babeltrace2/trace-ir/field-path-const.h>
40 #include <babeltrace2/value.h>
42 #include "common/assert.h"
43 #include "common/macros.h"
44 #include "common/safe.h"
46 #define BT_COMMON_COLOR_RESET "\033[0m"
47 #define BT_COMMON_COLOR_BOLD "\033[1m"
48 #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
49 #define BT_COMMON_COLOR_FG_RED "\033[31m"
50 #define BT_COMMON_COLOR_FG_GREEN "\033[32m"
51 #define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
52 #define BT_COMMON_COLOR_FG_BLUE "\033[34m"
53 #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
54 #define BT_COMMON_COLOR_FG_CYAN "\033[36m"
55 #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
56 #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
57 #define BT_COMMON_COLOR_BG_RED "\033[41m"
58 #define BT_COMMON_COLOR_BG_GREEN "\033[42m"
59 #define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
60 #define BT_COMMON_COLOR_BG_BLUE "\033[44m"
61 #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
62 #define BT_COMMON_COLOR_BG_CYAN "\033[46m"
63 #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
65 struct bt_common_lttng_live_url_parts
{
68 GString
*target_hostname
;
69 GString
*session_name
;
71 /* -1 means default port */
76 * Checks if the current process has setuid or setgid access rights.
77 * Returns `true` if so.
80 bool bt_common_is_setuid_setgid(void);
83 * Returns the system-wide plugin path, e.g.
84 * `/usr/lib/babeltrace2/plugins`. Do not free the return value.
87 const char *bt_common_get_system_plugin_path(void);
90 * Returns the user plugin path, e.g.
91 * `/home/user/.local/lib/babeltrace2/plugins`. You need to free the
95 char *bt_common_get_home_plugin_path(void);
98 * Appends the list of directories in `paths` to the array `dirs`.
99 * `paths` is a list of directories separated by `:`. Returns 0 on
103 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
);
106 * Returns `true` if terminal color codes are supported for this
110 bool bt_common_colors_supported(void);
113 const char *bt_common_color_reset(void);
116 const char *bt_common_color_bold(void);
119 const char *bt_common_color_fg_default(void);
122 const char *bt_common_color_fg_red(void);
125 const char *bt_common_color_fg_green(void);
128 const char *bt_common_color_fg_yellow(void);
131 const char *bt_common_color_fg_blue(void);
134 const char *bt_common_color_fg_magenta(void);
137 const char *bt_common_color_fg_cyan(void);
140 const char *bt_common_color_fg_light_gray(void);
143 const char *bt_common_color_bg_default(void);
146 const char *bt_common_color_bg_red(void);
149 const char *bt_common_color_bg_green(void);
152 const char *bt_common_color_bg_yellow(void);
155 const char *bt_common_color_bg_blue(void);
158 const char *bt_common_color_bg_magenta(void);
161 const char *bt_common_color_bg_cyan(void);
164 const char *bt_common_color_bg_light_gray(void);
167 * Returns the substring from `input` to the first character found
168 * in the list of characters `end_chars`, unescaping any character
169 * found in `escapable_chars`, and sets `*end_pos` to the position of
170 * the end (from `input`). The caller owns the returned GString.
173 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
174 const char *end_chars
, size_t *end_pos
);
177 * Returns the quoted version of `input` for a shell. If
178 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
179 * and suffix to the returned string; otherwise the caller should
180 * prepend and append them manually, although they are not always
181 * required. The caller owns the returned GString.
184 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
);
187 * Returns `true` if `input` is a string made only of printable
191 bool bt_common_string_is_printable(const char *input
);
194 * Destroys the parts of an LTTng live URL as returned by
195 * bt_common_parse_lttng_live_url().
198 void bt_common_destroy_lttng_live_url_parts(
199 struct bt_common_lttng_live_url_parts
*parts
);
202 * Parses the LTTng live URL `url` and returns its different parts.
203 * If there's an error, writes the error message into `*error_buf`
204 * up to `error_buf_size` bytes. You must destroy the returned value
205 * with bt_common_destroy_lttng_live_url_parts().
208 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
209 const char *url
, char *error_buf
, size_t error_buf_size
);
212 * Normalizes (in place) a star globbing pattern to be used with
213 * bt_common_star_glob_match(). This function always succeeds.
216 void bt_common_normalize_star_glob_pattern(char *pattern
);
219 * Returns `true` if `candidate` (of size `candidate_len`) matches
220 * the star globbing pattern `pattern` (of size `pattern_len`).
223 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
224 const char *candidate
, size_t candidate_len
);
227 * Normalizes the path `path`:
229 * * If it's a relative path, converts it to an absolute path using
230 * `wd` as the working directory (or the current working directory
232 * * Removes consecutive and trailing slashes.
233 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
234 * * Does NOT resolve symbolic links.
236 * The caller owns the returned GString.
239 GString
*bt_common_normalize_path(const char *path
, const char *wd
);
241 typedef void (* bt_common_handle_custom_specifier_func
)(void *priv_data
,
242 char **buf
, size_t avail_size
, const char **fmt
, va_list *args
);
245 * This is a custom vsnprintf() which handles the standard conversion
246 * specifier as well as custom ones.
248 * `fmt` is a typical printf()-style format string, with the following
251 * * The `*` width specifier is not accepted.
252 * * The `*` precision specifier is not accepted.
253 * * The `j` and `t` length modifiers are not accepted.
254 * * The `n` format specifier is not accepted.
255 * * The format specifiers defined in <inttypes.h> are not accepted
256 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
259 * `intro` specifies which special character immediately following an
260 * introductory `%` character in `fmt` is used to indicate a custom
261 * conversion specifier. For example, if `intro` is '@', then any `%@`
262 * sequence in `fmt` is the beginning of a custom conversion specifier.
264 * When a custom conversion specifier is encountered in `fmt`,
265 * the function calls `handle_specifier`. This callback receives:
268 * Custom, private data.
271 * Address of the current buffer pointer. `*buf` is the position to
272 * append new data. The callback must update `*buf` when appending
273 * new data. The callback must ensure not to write passed the whole
274 * buffer passed to bt_common_custom_vsnprintf().
277 * Number of bytes left in whole buffer from the `*buf` point.
280 * Address of the current format string pointer. `*fmt` points to
281 * the introductory `%` character, which is followed by the
282 * character `intro`. The callback must update `*fmt` so that it
283 * points after the whole custom conversion specifier.
286 * Variable argument list. Use va_arg() to get new arguments from
287 * this list and update it at the same time.
289 * Because this is an internal utility, this function and its callback
290 * do not return error codes: they abort when there's any error (bad
291 * format string, for example).
294 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
296 bt_common_handle_custom_specifier_func handle_specifier
,
297 void *priv_data
, const char *fmt
, va_list *args
);
300 * Variadic form of bt_common_custom_vsnprintf().
303 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
305 bt_common_handle_custom_specifier_func handle_specifier
,
306 void *priv_data
, const char *fmt
, ...);
309 * Returns the system page size.
312 size_t bt_common_get_page_size(void);
315 * Adds the digit separator `sep` as many times as needed to form groups
316 * of `digits_per_group` digits within `str`. `str` must have enough
317 * room to accomodate the new separators, that is:
319 * strlen(str) + (strlen(str) / digits_per_group) + 1
321 * Example: with `str` `1983198398213`, `digits_per_group` 3, and `sep`
322 * `,`, `str` becomes `1,983,198,398,213`.
324 * `strlen(str)` must not be 0. `digits_per_group` must not be 0. `sep`
328 void bt_common_sep_digits(char *str
, unsigned int digits_per_group
, char sep
);
331 * Wraps read() function to handle EINTR and partial reads.
332 * On success, it returns `count` received as parameter. On error, it returns a
333 * value smaller than the requested `count`.
336 ssize_t
bt_common_read(int fd
, void *buf
, size_t count
)
343 /* Never return an overflow value. */
344 BT_ASSERT(count
<= SSIZE_MAX
);
347 ret
= read(fd
, buf
+ i
, count
- i
);
349 if (errno
== EINTR
) {
351 BT_LOGD_STR("read() call interrupted; retrying...");
353 /* retry operation */
357 BT_LOGE_ERRNO("Error while reading", ": fd=%d",
364 BT_ASSERT(i
<= count
);
365 } while (count
- i
> 0 && ret
> 0);
380 const char *bt_common_field_class_type_string(enum bt_field_class_type class_type
)
382 switch (class_type
) {
383 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
384 return "BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER";
385 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
386 return "BT_FIELD_CLASS_TYPE_SIGNED_INTEGER";
387 case BT_FIELD_CLASS_TYPE_REAL
:
388 return "BT_FIELD_CLASS_TYPE_REAL";
389 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
390 return "BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION";
391 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
392 return "BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION";
393 case BT_FIELD_CLASS_TYPE_STRING
:
394 return "BT_FIELD_CLASS_TYPE_STRING";
395 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
396 return "BT_FIELD_CLASS_TYPE_STRUCTURE";
397 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
398 return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY";
399 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
400 return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY";
401 case BT_FIELD_CLASS_TYPE_VARIANT
:
402 return "BT_FIELD_CLASS_TYPE_VARIANT";
409 const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base
)
412 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
413 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
414 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
415 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
416 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
417 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
418 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
419 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
426 const char *bt_common_scope_string(enum bt_scope scope
)
429 case BT_SCOPE_PACKET_CONTEXT
:
430 return "BT_SCOPE_PACKET_CONTEXT";
431 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
432 return "BT_SCOPE_EVENT_COMMON_CONTEXT";
433 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
434 return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT";
435 case BT_SCOPE_EVENT_PAYLOAD
:
436 return "BT_SCOPE_EVENT_PAYLOAD";
443 const char *bt_common_event_class_log_level_string(
444 enum bt_event_class_log_level level
)
447 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
:
448 return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY";
449 case BT_EVENT_CLASS_LOG_LEVEL_ALERT
:
450 return "BT_EVENT_CLASS_LOG_LEVEL_ALERT";
451 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
:
452 return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL";
453 case BT_EVENT_CLASS_LOG_LEVEL_ERROR
:
454 return "BT_EVENT_CLASS_LOG_LEVEL_ERROR";
455 case BT_EVENT_CLASS_LOG_LEVEL_WARNING
:
456 return "BT_EVENT_CLASS_LOG_LEVEL_WARNING";
457 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE
:
458 return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE";
459 case BT_EVENT_CLASS_LOG_LEVEL_INFO
:
460 return "BT_EVENT_CLASS_LOG_LEVEL_INFO";
461 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
:
462 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM";
463 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
:
464 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM";
465 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
:
466 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS";
467 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
:
468 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE";
469 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
:
470 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT";
471 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
:
472 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION";
473 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
:
474 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE";
475 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG
:
476 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG";
483 const char *bt_common_value_type_string(enum bt_value_type type
)
486 case BT_VALUE_TYPE_NULL
:
487 return "BT_VALUE_TYPE_NULL";
488 case BT_VALUE_TYPE_BOOL
:
489 return "BT_VALUE_TYPE_BOOL";
490 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
491 return "BT_VALUE_TYPE_UNSIGNED_INTEGER";
492 case BT_VALUE_TYPE_SIGNED_INTEGER
:
493 return "BT_VALUE_TYPE_SIGNED_INTEGER";
494 case BT_VALUE_TYPE_REAL
:
495 return "BT_VALUE_TYPE_REAL";
496 case BT_VALUE_TYPE_STRING
:
497 return "BT_VALUE_TYPE_STRING";
498 case BT_VALUE_TYPE_ARRAY
:
499 return "BT_VALUE_TYPE_ARRAY";
500 case BT_VALUE_TYPE_MAP
:
501 return "BT_VALUE_TYPE_MAP";
508 GString
*bt_common_field_path_string(struct bt_field_path
*path
)
510 GString
*str
= g_string_new(NULL
);
519 g_string_append_printf(str
, "[%s", bt_common_scope_string(
520 bt_field_path_get_root_scope(path
)));
522 for (i
= 0; i
< bt_field_path_get_item_count(path
); i
++) {
523 const struct bt_field_path_item
*fp_item
=
524 bt_field_path_borrow_item_by_index_const(path
, i
);
526 switch (bt_field_path_item_get_type(fp_item
)) {
527 case BT_FIELD_PATH_ITEM_TYPE_INDEX
:
528 g_string_append_printf(str
, ", %" PRIu64
,
529 bt_field_path_item_index_get_index(fp_item
));
531 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
:
532 g_string_append(str
, ", <CUR>");
539 g_string_append(str
, "]");
546 const char *bt_common_self_message_iterator_status_string(
547 enum bt_self_message_iterator_status status
)
550 case BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN
:
551 return "BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN";
552 case BT_SELF_MESSAGE_ITERATOR_STATUS_END
:
553 return "BT_SELF_MESSAGE_ITERATOR_STATUS_END";
554 case BT_SELF_MESSAGE_ITERATOR_STATUS_OK
:
555 return "BT_SELF_MESSAGE_ITERATOR_STATUS_OK";
556 case BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
:
557 return "BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR";
558 case BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
:
559 return "BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM";
565 #define NS_PER_S_I INT64_C(1000000000)
566 #define NS_PER_S_U UINT64_C(1000000000)
569 int bt_common_clock_value_from_ns_from_origin(
570 int64_t cc_offset_seconds
, uint64_t cc_offset_cycles
,
571 uint64_t cc_freq
, int64_t ns_from_origin
,
575 int64_t offset_in_ns
;
576 uint64_t value_in_ns
;
577 uint64_t rem_value_in_ns
;
578 uint64_t value_periods
;
579 uint64_t value_period_cycles
;
582 BT_ASSERT(raw_value
);
584 /* Compute offset part of requested value, in nanoseconds */
585 if (!bt_safe_to_mul_int64(cc_offset_seconds
, NS_PER_S_I
)) {
590 offset_in_ns
= cc_offset_seconds
* NS_PER_S_I
;
592 if (cc_freq
== NS_PER_S_U
) {
593 ns_to_add
= (int64_t) cc_offset_cycles
;
595 if (!bt_safe_to_mul_int64((int64_t) cc_offset_cycles
,
601 ns_to_add
= ((int64_t) cc_offset_cycles
* NS_PER_S_I
) /
605 if (!bt_safe_to_add_int64(offset_in_ns
, ns_to_add
)) {
610 offset_in_ns
+= ns_to_add
;
612 /* Value part in nanoseconds */
613 if (ns_from_origin
< offset_in_ns
) {
618 value_in_ns
= (uint64_t) (ns_from_origin
- offset_in_ns
);
620 /* Number of whole clock periods in `value_in_ns` */
621 value_periods
= value_in_ns
/ NS_PER_S_U
;
623 /* Remaining nanoseconds in cycles + whole clock periods in cycles */
624 rem_value_in_ns
= value_in_ns
- value_periods
* NS_PER_S_U
;
626 if (value_periods
> UINT64_MAX
/ cc_freq
) {
631 if (!bt_safe_to_mul_uint64(value_periods
, cc_freq
)) {
636 value_period_cycles
= value_periods
* cc_freq
;
638 if (!bt_safe_to_mul_uint64(cc_freq
, rem_value_in_ns
)) {
643 if (!bt_safe_to_add_uint64(cc_freq
* rem_value_in_ns
/ NS_PER_S_U
,
644 value_period_cycles
)) {
649 *raw_value
= cc_freq
* rem_value_in_ns
/ NS_PER_S_U
+
657 enum bt_self_message_iterator_status
bt_common_message_iterator_status_to_self(
658 enum bt_message_iterator_status status
)
663 #endif /* BABELTRACE_COMMON_INTERNAL_H */