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/logging.h>
41 #include <babeltrace2/value.h>
43 #include "common/assert.h"
44 #include "common/macros.h"
45 #include "common/safe.h"
47 #define BT_COMMON_COLOR_RESET "\033[0m"
48 #define BT_COMMON_COLOR_BOLD "\033[1m"
49 #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
50 #define BT_COMMON_COLOR_FG_RED "\033[31m"
51 #define BT_COMMON_COLOR_FG_GREEN "\033[32m"
52 #define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
53 #define BT_COMMON_COLOR_FG_BLUE "\033[34m"
54 #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
55 #define BT_COMMON_COLOR_FG_CYAN "\033[36m"
56 #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
57 #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
58 #define BT_COMMON_COLOR_BG_RED "\033[41m"
59 #define BT_COMMON_COLOR_BG_GREEN "\033[42m"
60 #define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
61 #define BT_COMMON_COLOR_BG_BLUE "\033[44m"
62 #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
63 #define BT_COMMON_COLOR_BG_CYAN "\033[46m"
64 #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
66 struct bt_common_lttng_live_url_parts
{
69 GString
*target_hostname
;
70 GString
*session_name
;
72 /* -1 means default port */
77 * Checks if the current process has setuid or setgid access rights.
78 * Returns `true` if so.
81 bool bt_common_is_setuid_setgid(void);
84 * Returns the system-wide plugin path, e.g.
85 * `/usr/lib/babeltrace2/plugins`. Do not free the return value.
88 const char *bt_common_get_system_plugin_path(void);
91 * Returns the user plugin path, e.g.
92 * `/home/user/.local/lib/babeltrace2/plugins`. You need to free the
96 char *bt_common_get_home_plugin_path(void);
99 * Appends the list of directories in `paths` to the array `dirs`.
100 * `paths` is a list of directories separated by `:`. Returns 0 on
104 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
);
107 * Returns `true` if terminal color codes are supported for this
111 bool bt_common_colors_supported(void);
114 const char *bt_common_color_reset(void);
117 const char *bt_common_color_bold(void);
120 const char *bt_common_color_fg_default(void);
123 const char *bt_common_color_fg_red(void);
126 const char *bt_common_color_fg_green(void);
129 const char *bt_common_color_fg_yellow(void);
132 const char *bt_common_color_fg_blue(void);
135 const char *bt_common_color_fg_magenta(void);
138 const char *bt_common_color_fg_cyan(void);
141 const char *bt_common_color_fg_light_gray(void);
144 const char *bt_common_color_bg_default(void);
147 const char *bt_common_color_bg_red(void);
150 const char *bt_common_color_bg_green(void);
153 const char *bt_common_color_bg_yellow(void);
156 const char *bt_common_color_bg_blue(void);
159 const char *bt_common_color_bg_magenta(void);
162 const char *bt_common_color_bg_cyan(void);
165 const char *bt_common_color_bg_light_gray(void);
168 * Returns the substring from `input` to the first character found
169 * in the list of characters `end_chars`, unescaping any character
170 * found in `escapable_chars`, and sets `*end_pos` to the position of
171 * the end (from `input`). The caller owns the returned GString.
174 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
175 const char *end_chars
, size_t *end_pos
);
178 * Returns the quoted version of `input` for a shell. If
179 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
180 * and suffix to the returned string; otherwise the caller should
181 * prepend and append them manually, although they are not always
182 * required. The caller owns the returned GString.
185 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
);
188 * Returns `true` if `input` is a string made only of printable
192 bool bt_common_string_is_printable(const char *input
);
195 * Destroys the parts of an LTTng live URL as returned by
196 * bt_common_parse_lttng_live_url().
199 void bt_common_destroy_lttng_live_url_parts(
200 struct bt_common_lttng_live_url_parts
*parts
);
203 * Parses the LTTng live URL `url` and returns its different parts.
204 * If there's an error, writes the error message into `*error_buf`
205 * up to `error_buf_size` bytes. You must destroy the returned value
206 * with bt_common_destroy_lttng_live_url_parts().
209 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
210 const char *url
, char *error_buf
, size_t error_buf_size
);
213 * Normalizes (in place) a star globbing pattern to be used with
214 * bt_common_star_glob_match(). This function always succeeds.
217 void bt_common_normalize_star_glob_pattern(char *pattern
);
220 * Returns `true` if `candidate` (of size `candidate_len`) matches
221 * the star globbing pattern `pattern` (of size `pattern_len`).
224 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
225 const char *candidate
, size_t candidate_len
);
228 * Normalizes the path `path`:
230 * * If it's a relative path, converts it to an absolute path using
231 * `wd` as the working directory (or the current working directory
233 * * Removes consecutive and trailing slashes.
234 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
235 * * Does NOT resolve symbolic links.
237 * The caller owns the returned GString.
240 GString
*bt_common_normalize_path(const char *path
, const char *wd
);
242 typedef void (* bt_common_handle_custom_specifier_func
)(void *priv_data
,
243 char **buf
, size_t avail_size
, const char **fmt
, va_list *args
);
246 * This is a custom vsnprintf() which handles the standard conversion
247 * specifier as well as custom ones.
249 * `fmt` is a typical printf()-style format string, with the following
252 * * The `*` width specifier is not accepted.
253 * * The `*` precision specifier is not accepted.
254 * * The `j` and `t` length modifiers are not accepted.
255 * * The `n` format specifier is not accepted.
256 * * The format specifiers defined in <inttypes.h> are not accepted
257 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
260 * `intro` specifies which special character immediately following an
261 * introductory `%` character in `fmt` is used to indicate a custom
262 * conversion specifier. For example, if `intro` is '@', then any `%@`
263 * sequence in `fmt` is the beginning of a custom conversion specifier.
265 * When a custom conversion specifier is encountered in `fmt`,
266 * the function calls `handle_specifier`. This callback receives:
269 * Custom, private data.
272 * Address of the current buffer pointer. `*buf` is the position to
273 * append new data. The callback must update `*buf` when appending
274 * new data. The callback must ensure not to write passed the whole
275 * buffer passed to bt_common_custom_vsnprintf().
278 * Number of bytes left in whole buffer from the `*buf` point.
281 * Address of the current format string pointer. `*fmt` points to
282 * the introductory `%` character, which is followed by the
283 * character `intro`. The callback must update `*fmt` so that it
284 * points after the whole custom conversion specifier.
287 * Variable argument list. Use va_arg() to get new arguments from
288 * this list and update it at the same time.
290 * Because this is an internal utility, this function and its callback
291 * do not return error codes: they abort when there's any error (bad
292 * format string, for example).
295 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
297 bt_common_handle_custom_specifier_func handle_specifier
,
298 void *priv_data
, const char *fmt
, va_list *args
);
301 * Variadic form of bt_common_custom_vsnprintf().
304 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
306 bt_common_handle_custom_specifier_func handle_specifier
,
307 void *priv_data
, const char *fmt
, ...);
310 * Returns the system page size.
313 size_t bt_common_get_page_size(void);
316 * Adds the digit separator `sep` as many times as needed to form groups
317 * of `digits_per_group` digits within `str`. `str` must have enough
318 * room to accomodate the new separators, that is:
320 * strlen(str) + (strlen(str) / digits_per_group) + 1
322 * Example: with `str` `1983198398213`, `digits_per_group` 3, and `sep`
323 * `,`, `str` becomes `1,983,198,398,213`.
325 * `strlen(str)` must not be 0. `digits_per_group` must not be 0. `sep`
329 void bt_common_sep_digits(char *str
, unsigned int digits_per_group
, char sep
);
332 * Wraps read() function to handle EINTR and partial reads.
333 * On success, it returns `count` received as parameter. On error, it returns a
334 * value smaller than the requested `count`.
337 ssize_t
bt_common_read(int fd
, void *buf
, size_t count
)
344 /* Never return an overflow value. */
345 BT_ASSERT(count
<= SSIZE_MAX
);
348 ret
= read(fd
, buf
+ i
, count
- i
);
350 if (errno
== EINTR
) {
352 BT_LOGD_STR("read() call interrupted; retrying...");
354 /* retry operation */
358 BT_LOGE_ERRNO("Error while reading", ": fd=%d",
365 BT_ASSERT(i
<= count
);
366 } while (count
- i
> 0 && ret
> 0);
381 const char *bt_common_field_class_type_string(enum bt_field_class_type class_type
)
383 switch (class_type
) {
384 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
385 return "BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER";
386 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
387 return "BT_FIELD_CLASS_TYPE_SIGNED_INTEGER";
388 case BT_FIELD_CLASS_TYPE_REAL
:
389 return "BT_FIELD_CLASS_TYPE_REAL";
390 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
391 return "BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION";
392 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
393 return "BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION";
394 case BT_FIELD_CLASS_TYPE_STRING
:
395 return "BT_FIELD_CLASS_TYPE_STRING";
396 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
397 return "BT_FIELD_CLASS_TYPE_STRUCTURE";
398 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
399 return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY";
400 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
401 return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY";
402 case BT_FIELD_CLASS_TYPE_VARIANT
:
403 return "BT_FIELD_CLASS_TYPE_VARIANT";
410 const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base
)
413 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
414 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
415 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
416 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
417 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
418 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
419 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
420 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
427 const char *bt_common_scope_string(enum bt_scope scope
)
430 case BT_SCOPE_PACKET_CONTEXT
:
431 return "BT_SCOPE_PACKET_CONTEXT";
432 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
433 return "BT_SCOPE_EVENT_COMMON_CONTEXT";
434 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
435 return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT";
436 case BT_SCOPE_EVENT_PAYLOAD
:
437 return "BT_SCOPE_EVENT_PAYLOAD";
444 const char *bt_common_event_class_log_level_string(
445 enum bt_event_class_log_level level
)
448 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
:
449 return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY";
450 case BT_EVENT_CLASS_LOG_LEVEL_ALERT
:
451 return "BT_EVENT_CLASS_LOG_LEVEL_ALERT";
452 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
:
453 return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL";
454 case BT_EVENT_CLASS_LOG_LEVEL_ERROR
:
455 return "BT_EVENT_CLASS_LOG_LEVEL_ERROR";
456 case BT_EVENT_CLASS_LOG_LEVEL_WARNING
:
457 return "BT_EVENT_CLASS_LOG_LEVEL_WARNING";
458 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE
:
459 return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE";
460 case BT_EVENT_CLASS_LOG_LEVEL_INFO
:
461 return "BT_EVENT_CLASS_LOG_LEVEL_INFO";
462 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
:
463 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM";
464 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
:
465 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM";
466 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
:
467 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS";
468 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
:
469 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE";
470 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
:
471 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT";
472 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
:
473 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION";
474 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
:
475 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE";
476 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG
:
477 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG";
484 const char *bt_common_value_type_string(enum bt_value_type type
)
487 case BT_VALUE_TYPE_NULL
:
488 return "BT_VALUE_TYPE_NULL";
489 case BT_VALUE_TYPE_BOOL
:
490 return "BT_VALUE_TYPE_BOOL";
491 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
492 return "BT_VALUE_TYPE_UNSIGNED_INTEGER";
493 case BT_VALUE_TYPE_SIGNED_INTEGER
:
494 return "BT_VALUE_TYPE_SIGNED_INTEGER";
495 case BT_VALUE_TYPE_REAL
:
496 return "BT_VALUE_TYPE_REAL";
497 case BT_VALUE_TYPE_STRING
:
498 return "BT_VALUE_TYPE_STRING";
499 case BT_VALUE_TYPE_ARRAY
:
500 return "BT_VALUE_TYPE_ARRAY";
501 case BT_VALUE_TYPE_MAP
:
502 return "BT_VALUE_TYPE_MAP";
509 GString
*bt_common_field_path_string(struct bt_field_path
*path
)
511 GString
*str
= g_string_new(NULL
);
520 g_string_append_printf(str
, "[%s", bt_common_scope_string(
521 bt_field_path_get_root_scope(path
)));
523 for (i
= 0; i
< bt_field_path_get_item_count(path
); i
++) {
524 const struct bt_field_path_item
*fp_item
=
525 bt_field_path_borrow_item_by_index_const(path
, i
);
527 switch (bt_field_path_item_get_type(fp_item
)) {
528 case BT_FIELD_PATH_ITEM_TYPE_INDEX
:
529 g_string_append_printf(str
, ", %" PRIu64
,
530 bt_field_path_item_index_get_index(fp_item
));
532 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
:
533 g_string_append(str
, ", <CUR>");
540 g_string_append(str
, "]");
547 const char *bt_common_self_message_iterator_status_string(
548 enum bt_self_message_iterator_status status
)
551 case BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN
:
552 return "BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN";
553 case BT_SELF_MESSAGE_ITERATOR_STATUS_END
:
554 return "BT_SELF_MESSAGE_ITERATOR_STATUS_END";
555 case BT_SELF_MESSAGE_ITERATOR_STATUS_OK
:
556 return "BT_SELF_MESSAGE_ITERATOR_STATUS_OK";
557 case BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR
:
558 return "BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR";
559 case BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM
:
560 return "BT_SELF_MESSAGE_ITERATOR_STATUS_NOMEM";
567 const char *bt_common_logging_level_string(
568 enum bt_logging_level level
)
571 case BT_LOGGING_LEVEL_VERBOSE
:
572 return "BT_LOGGING_LEVEL_VERBOSE";
573 case BT_LOGGING_LEVEL_DEBUG
:
574 return "BT_LOGGING_LEVEL_DEBUG";
575 case BT_LOGGING_LEVEL_INFO
:
576 return "BT_LOGGING_LEVEL_INFO";
577 case BT_LOGGING_LEVEL_WARN
:
578 return "BT_LOGGING_LEVEL_WARN";
579 case BT_LOGGING_LEVEL_ERROR
:
580 return "BT_LOGGING_LEVEL_ERROR";
581 case BT_LOGGING_LEVEL_FATAL
:
582 return "BT_LOGGING_LEVEL_FATAL";
583 case BT_LOGGING_LEVEL_NONE
:
584 return "BT_LOGGING_LEVEL_NONE";
590 #define NS_PER_S_I INT64_C(1000000000)
591 #define NS_PER_S_U UINT64_C(1000000000)
594 int bt_common_clock_value_from_ns_from_origin(
595 int64_t cc_offset_seconds
, uint64_t cc_offset_cycles
,
596 uint64_t cc_freq
, int64_t ns_from_origin
,
600 int64_t offset_in_ns
;
601 uint64_t value_in_ns
;
602 uint64_t rem_value_in_ns
;
603 uint64_t value_periods
;
604 uint64_t value_period_cycles
;
607 BT_ASSERT(raw_value
);
609 /* Compute offset part of requested value, in nanoseconds */
610 if (!bt_safe_to_mul_int64(cc_offset_seconds
, NS_PER_S_I
)) {
615 offset_in_ns
= cc_offset_seconds
* NS_PER_S_I
;
617 if (cc_freq
== NS_PER_S_U
) {
618 ns_to_add
= (int64_t) cc_offset_cycles
;
620 if (!bt_safe_to_mul_int64((int64_t) cc_offset_cycles
,
626 ns_to_add
= ((int64_t) cc_offset_cycles
* NS_PER_S_I
) /
630 if (!bt_safe_to_add_int64(offset_in_ns
, ns_to_add
)) {
635 offset_in_ns
+= ns_to_add
;
637 /* Value part in nanoseconds */
638 if (ns_from_origin
< offset_in_ns
) {
643 value_in_ns
= (uint64_t) (ns_from_origin
- offset_in_ns
);
645 /* Number of whole clock periods in `value_in_ns` */
646 value_periods
= value_in_ns
/ NS_PER_S_U
;
648 /* Remaining nanoseconds in cycles + whole clock periods in cycles */
649 rem_value_in_ns
= value_in_ns
- value_periods
* NS_PER_S_U
;
651 if (value_periods
> UINT64_MAX
/ cc_freq
) {
656 if (!bt_safe_to_mul_uint64(value_periods
, cc_freq
)) {
661 value_period_cycles
= value_periods
* cc_freq
;
663 if (!bt_safe_to_mul_uint64(cc_freq
, rem_value_in_ns
)) {
668 if (!bt_safe_to_add_uint64(cc_freq
* rem_value_in_ns
/ NS_PER_S_U
,
669 value_period_cycles
)) {
674 *raw_value
= cc_freq
* rem_value_in_ns
/ NS_PER_S_U
+
682 enum bt_self_message_iterator_status
bt_common_message_iterator_status_to_self(
683 enum bt_message_iterator_status status
)
688 #endif /* BABELTRACE_COMMON_INTERNAL_H */