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/trace-ir/event-class-const.h>
37 #include <babeltrace2/trace-ir/field-class-const.h>
38 #include <babeltrace2/trace-ir/field-path-const.h>
39 #include <babeltrace2/logging.h>
40 #include <babeltrace2/value.h>
42 #define __BT_FUNC_STATUS_ENABLE
43 #include <babeltrace2/func-status.h>
44 #undef __BT_FUNC_STATUS_ENABLE
46 #include "common/assert.h"
47 #include "common/macros.h"
48 #include "common/safe.h"
50 #define BT_COMMON_COLOR_RESET "\033[0m"
51 #define BT_COMMON_COLOR_BOLD "\033[1m"
52 #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
53 #define BT_COMMON_COLOR_FG_RED "\033[31m"
54 #define BT_COMMON_COLOR_FG_GREEN "\033[32m"
55 #define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
56 #define BT_COMMON_COLOR_FG_BLUE "\033[34m"
57 #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
58 #define BT_COMMON_COLOR_FG_CYAN "\033[36m"
59 #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
60 #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
61 #define BT_COMMON_COLOR_BG_RED "\033[41m"
62 #define BT_COMMON_COLOR_BG_GREEN "\033[42m"
63 #define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
64 #define BT_COMMON_COLOR_BG_BLUE "\033[44m"
65 #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
66 #define BT_COMMON_COLOR_BG_CYAN "\033[46m"
67 #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
69 struct bt_common_lttng_live_url_parts
{
72 GString
*target_hostname
;
73 GString
*session_name
;
75 /* -1 means default port */
80 * Checks if the current process has setuid or setgid access rights.
81 * Returns `true` if so.
84 bool bt_common_is_setuid_setgid(void);
87 * Returns the system-wide plugin path, e.g.
88 * `/usr/lib/babeltrace2/plugins`. Do not free the return value.
91 const char *bt_common_get_system_plugin_path(void);
94 * Returns the user plugin path, e.g.
95 * `/home/user/.local/lib/babeltrace2/plugins`. You need to free the
99 char *bt_common_get_home_plugin_path(int log_level
);
102 * Appends the list of directories in `paths` to the array `dirs`.
103 * `paths` is a list of directories separated by `:`. Returns 0 on
107 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
);
110 * Returns `true` if terminal color codes are supported for this
114 bool bt_common_colors_supported(void);
117 const char *bt_common_color_reset(void);
120 const char *bt_common_color_bold(void);
123 const char *bt_common_color_fg_default(void);
126 const char *bt_common_color_fg_red(void);
129 const char *bt_common_color_fg_green(void);
132 const char *bt_common_color_fg_yellow(void);
135 const char *bt_common_color_fg_blue(void);
138 const char *bt_common_color_fg_magenta(void);
141 const char *bt_common_color_fg_cyan(void);
144 const char *bt_common_color_fg_light_gray(void);
147 const char *bt_common_color_bg_default(void);
150 const char *bt_common_color_bg_red(void);
153 const char *bt_common_color_bg_green(void);
156 const char *bt_common_color_bg_yellow(void);
159 const char *bt_common_color_bg_blue(void);
162 const char *bt_common_color_bg_magenta(void);
165 const char *bt_common_color_bg_cyan(void);
168 const char *bt_common_color_bg_light_gray(void);
171 * Returns the substring from `input` to the first character found
172 * in the list of characters `end_chars`, unescaping any character
173 * found in `escapable_chars`, and sets `*end_pos` to the position of
174 * the end (from `input`). The caller owns the returned GString.
177 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
178 const char *end_chars
, size_t *end_pos
);
181 * Returns the quoted version of `input` for a shell. If
182 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
183 * and suffix to the returned string; otherwise the caller should
184 * prepend and append them manually, although they are not always
185 * required. The caller owns the returned GString.
188 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
);
191 * Returns `true` if `input` is a string made only of printable
195 bool bt_common_string_is_printable(const char *input
);
198 * Destroys the parts of an LTTng live URL as returned by
199 * bt_common_parse_lttng_live_url().
202 void bt_common_destroy_lttng_live_url_parts(
203 struct bt_common_lttng_live_url_parts
*parts
);
206 * Parses the LTTng live URL `url` and returns its different parts.
207 * If there's an error, writes the error message into `*error_buf`
208 * up to `error_buf_size` bytes. You must destroy the returned value
209 * with bt_common_destroy_lttng_live_url_parts().
212 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
213 const char *url
, char *error_buf
, size_t error_buf_size
);
216 * Normalizes (in place) a star globbing pattern to be used with
217 * bt_common_star_glob_match(). This function always succeeds.
220 void bt_common_normalize_star_glob_pattern(char *pattern
);
223 * Returns `true` if `candidate` (of size `candidate_len`) matches
224 * the star globbing pattern `pattern` (of size `pattern_len`).
227 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
228 const char *candidate
, size_t candidate_len
);
231 * Normalizes the path `path`:
233 * * If it's a relative path, converts it to an absolute path using
234 * `wd` as the working directory (or the current working directory
236 * * Removes consecutive and trailing slashes.
237 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
238 * * Does NOT resolve symbolic links.
240 * The caller owns the returned GString.
243 GString
*bt_common_normalize_path(const char *path
, const char *wd
);
245 typedef void (* bt_common_handle_custom_specifier_func
)(void *priv_data
,
246 char **buf
, size_t avail_size
, const char **fmt
, va_list *args
);
249 * This is a custom vsnprintf() which handles the standard conversion
250 * specifier as well as custom ones.
252 * `fmt` is a typical printf()-style format string, with the following
255 * * The `*` width specifier is not accepted.
256 * * The `*` precision specifier is not accepted.
257 * * The `j` and `t` length modifiers are not accepted.
258 * * The `n` format specifier is not accepted.
259 * * The format specifiers defined in <inttypes.h> are not accepted
260 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
263 * `intro` specifies which special character immediately following an
264 * introductory `%` character in `fmt` is used to indicate a custom
265 * conversion specifier. For example, if `intro` is '@', then any `%@`
266 * sequence in `fmt` is the beginning of a custom conversion specifier.
268 * When a custom conversion specifier is encountered in `fmt`,
269 * the function calls `handle_specifier`. This callback receives:
272 * Custom, private data.
275 * Address of the current buffer pointer. `*buf` is the position to
276 * append new data. The callback must update `*buf` when appending
277 * new data. The callback must ensure not to write passed the whole
278 * buffer passed to bt_common_custom_vsnprintf().
281 * Number of bytes left in whole buffer from the `*buf` point.
284 * Address of the current format string pointer. `*fmt` points to
285 * the introductory `%` character, which is followed by the
286 * character `intro`. The callback must update `*fmt` so that it
287 * points after the whole custom conversion specifier.
290 * Variable argument list. Use va_arg() to get new arguments from
291 * this list and update it at the same time.
293 * Because this is an internal utility, this function and its callback
294 * do not return error codes: they abort when there's any error (bad
295 * format string, for example).
298 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
300 bt_common_handle_custom_specifier_func handle_specifier
,
301 void *priv_data
, const char *fmt
, va_list *args
);
304 * Variadic form of bt_common_custom_vsnprintf().
307 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
309 bt_common_handle_custom_specifier_func handle_specifier
,
310 void *priv_data
, const char *fmt
, ...);
313 * Returns the system page size.
316 size_t bt_common_get_page_size(int log_level
);
319 * Adds the digit separator `sep` as many times as needed to form groups
320 * of `digits_per_group` digits within `str`. `str` must have enough
321 * room to accomodate the new separators, that is:
323 * strlen(str) + (strlen(str) / digits_per_group) + 1
325 * Example: with `str` `1983198398213`, `digits_per_group` 3, and `sep`
326 * `,`, `str` becomes `1,983,198,398,213`.
328 * `strlen(str)` must not be 0. `digits_per_group` must not be 0. `sep`
332 void bt_common_sep_digits(char *str
, unsigned int digits_per_group
, char sep
);
335 * Wraps read() function to handle EINTR and partial reads.
336 * On success, it returns `count` received as parameter. On error, it returns a
337 * value smaller than the requested `count`.
340 ssize_t
bt_common_read(int fd
, void *buf
, size_t count
, int log_level
)
347 /* Never return an overflow value. */
348 BT_ASSERT(count
<= SSIZE_MAX
);
351 ret
= read(fd
, buf
+ i
, count
- i
);
353 if (errno
== EINTR
) {
354 #ifdef BT_LOG_WRITE_CUR_LVL
355 BT_LOG_WRITE_CUR_LVL(BT_LOG_DEBUG
, log_level
,
357 "read() call interrupted; retrying...");
359 /* retry operation */
362 #ifdef BT_LOG_WRITE_ERRNO_CUR_LVL
363 BT_LOG_WRITE_ERRNO_CUR_LVL(BT_LOG_ERROR
,
364 log_level
, BT_LOG_TAG
,
365 "Error while reading", ": fd=%d", fd
);
371 BT_ASSERT(i
<= count
);
372 } while (count
- i
> 0 && ret
> 0);
387 const char *bt_common_field_class_type_string(enum bt_field_class_type class_type
)
389 switch (class_type
) {
390 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
391 return "BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER";
392 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
393 return "BT_FIELD_CLASS_TYPE_SIGNED_INTEGER";
394 case BT_FIELD_CLASS_TYPE_REAL
:
395 return "BT_FIELD_CLASS_TYPE_REAL";
396 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
397 return "BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION";
398 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
399 return "BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION";
400 case BT_FIELD_CLASS_TYPE_STRING
:
401 return "BT_FIELD_CLASS_TYPE_STRING";
402 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
403 return "BT_FIELD_CLASS_TYPE_STRUCTURE";
404 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
405 return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY";
406 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
407 return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY";
408 case BT_FIELD_CLASS_TYPE_VARIANT
:
409 return "BT_FIELD_CLASS_TYPE_VARIANT";
416 const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base
)
419 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
420 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
421 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
422 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
423 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
424 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
425 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
426 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
433 const char *bt_common_scope_string(enum bt_scope scope
)
436 case BT_SCOPE_PACKET_CONTEXT
:
437 return "BT_SCOPE_PACKET_CONTEXT";
438 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
439 return "BT_SCOPE_EVENT_COMMON_CONTEXT";
440 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
441 return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT";
442 case BT_SCOPE_EVENT_PAYLOAD
:
443 return "BT_SCOPE_EVENT_PAYLOAD";
450 const char *bt_common_event_class_log_level_string(
451 enum bt_event_class_log_level level
)
454 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
:
455 return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY";
456 case BT_EVENT_CLASS_LOG_LEVEL_ALERT
:
457 return "BT_EVENT_CLASS_LOG_LEVEL_ALERT";
458 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
:
459 return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL";
460 case BT_EVENT_CLASS_LOG_LEVEL_ERROR
:
461 return "BT_EVENT_CLASS_LOG_LEVEL_ERROR";
462 case BT_EVENT_CLASS_LOG_LEVEL_WARNING
:
463 return "BT_EVENT_CLASS_LOG_LEVEL_WARNING";
464 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE
:
465 return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE";
466 case BT_EVENT_CLASS_LOG_LEVEL_INFO
:
467 return "BT_EVENT_CLASS_LOG_LEVEL_INFO";
468 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
:
469 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM";
470 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
:
471 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM";
472 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
:
473 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS";
474 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
:
475 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE";
476 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
:
477 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT";
478 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
:
479 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION";
480 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
:
481 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE";
482 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG
:
483 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG";
490 const char *bt_common_value_type_string(enum bt_value_type type
)
493 case BT_VALUE_TYPE_NULL
:
494 return "BT_VALUE_TYPE_NULL";
495 case BT_VALUE_TYPE_BOOL
:
496 return "BT_VALUE_TYPE_BOOL";
497 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
498 return "BT_VALUE_TYPE_UNSIGNED_INTEGER";
499 case BT_VALUE_TYPE_SIGNED_INTEGER
:
500 return "BT_VALUE_TYPE_SIGNED_INTEGER";
501 case BT_VALUE_TYPE_REAL
:
502 return "BT_VALUE_TYPE_REAL";
503 case BT_VALUE_TYPE_STRING
:
504 return "BT_VALUE_TYPE_STRING";
505 case BT_VALUE_TYPE_ARRAY
:
506 return "BT_VALUE_TYPE_ARRAY";
507 case BT_VALUE_TYPE_MAP
:
508 return "BT_VALUE_TYPE_MAP";
515 GString
*bt_common_field_path_string(struct bt_field_path
*path
)
517 GString
*str
= g_string_new(NULL
);
526 g_string_append_printf(str
, "[%s", bt_common_scope_string(
527 bt_field_path_get_root_scope(path
)));
529 for (i
= 0; i
< bt_field_path_get_item_count(path
); i
++) {
530 const struct bt_field_path_item
*fp_item
=
531 bt_field_path_borrow_item_by_index_const(path
, i
);
533 switch (bt_field_path_item_get_type(fp_item
)) {
534 case BT_FIELD_PATH_ITEM_TYPE_INDEX
:
535 g_string_append_printf(str
, ", %" PRIu64
,
536 bt_field_path_item_index_get_index(fp_item
));
538 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
:
539 g_string_append(str
, ", <CUR>");
546 g_string_append(str
, "]");
553 const char *bt_common_logging_level_string(
554 enum bt_logging_level level
)
557 case BT_LOGGING_LEVEL_TRACE
:
558 return "BT_LOGGING_LEVEL_TRACE";
559 case BT_LOGGING_LEVEL_DEBUG
:
560 return "BT_LOGGING_LEVEL_DEBUG";
561 case BT_LOGGING_LEVEL_INFO
:
562 return "BT_LOGGING_LEVEL_INFO";
563 case BT_LOGGING_LEVEL_WARN
:
564 return "BT_LOGGING_LEVEL_WARN";
565 case BT_LOGGING_LEVEL_ERROR
:
566 return "BT_LOGGING_LEVEL_ERROR";
567 case BT_LOGGING_LEVEL_FATAL
:
568 return "BT_LOGGING_LEVEL_FATAL";
569 case BT_LOGGING_LEVEL_NONE
:
570 return "BT_LOGGING_LEVEL_NONE";
577 const char *bt_common_func_status_string(int status
)
580 case __BT_FUNC_STATUS_OVERFLOW
:
582 case __BT_FUNC_STATUS_INVALID_PARAMS
:
583 return "INVALID_PARAMS";
584 case __BT_FUNC_STATUS_INVALID_OBJECT
:
585 return "INVALID_OBJECT";
586 case __BT_FUNC_STATUS_MEMORY_ERROR
:
587 return "MEMORY_ERROR";
588 case __BT_FUNC_STATUS_LOADING_ERROR
:
589 return "LOADING_ERROR";
590 case __BT_FUNC_STATUS_ERROR
:
592 case __BT_FUNC_STATUS_OK
:
594 case __BT_FUNC_STATUS_END
:
596 case __BT_FUNC_STATUS_NOT_FOUND
:
598 case __BT_FUNC_STATUS_AGAIN
:
600 case __BT_FUNC_STATUS_UNSUPPORTED
:
601 return "UNSUPPORTED";
602 case __BT_FUNC_STATUS_CANCELED
:
609 #define NS_PER_S_I INT64_C(1000000000)
610 #define NS_PER_S_U UINT64_C(1000000000)
613 int bt_common_clock_value_from_ns_from_origin(
614 int64_t cc_offset_seconds
, uint64_t cc_offset_cycles
,
615 uint64_t cc_freq
, int64_t ns_from_origin
,
619 int64_t offset_in_ns
;
620 uint64_t value_in_ns
;
621 uint64_t rem_value_in_ns
;
622 uint64_t value_periods
;
623 uint64_t value_period_cycles
;
626 BT_ASSERT(raw_value
);
628 /* Compute offset part of requested value, in nanoseconds */
629 if (!bt_safe_to_mul_int64(cc_offset_seconds
, NS_PER_S_I
)) {
634 offset_in_ns
= cc_offset_seconds
* NS_PER_S_I
;
636 if (cc_freq
== NS_PER_S_U
) {
637 ns_to_add
= (int64_t) cc_offset_cycles
;
639 if (!bt_safe_to_mul_int64((int64_t) cc_offset_cycles
,
645 ns_to_add
= ((int64_t) cc_offset_cycles
* NS_PER_S_I
) /
649 if (!bt_safe_to_add_int64(offset_in_ns
, ns_to_add
)) {
654 offset_in_ns
+= ns_to_add
;
656 /* Value part in nanoseconds */
657 if (ns_from_origin
< offset_in_ns
) {
662 value_in_ns
= (uint64_t) (ns_from_origin
- offset_in_ns
);
664 /* Number of whole clock periods in `value_in_ns` */
665 value_periods
= value_in_ns
/ NS_PER_S_U
;
667 /* Remaining nanoseconds in cycles + whole clock periods in cycles */
668 rem_value_in_ns
= value_in_ns
- value_periods
* NS_PER_S_U
;
670 if (value_periods
> UINT64_MAX
/ cc_freq
) {
675 if (!bt_safe_to_mul_uint64(value_periods
, cc_freq
)) {
680 value_period_cycles
= value_periods
* cc_freq
;
682 if (!bt_safe_to_mul_uint64(cc_freq
, rem_value_in_ns
)) {
687 if (!bt_safe_to_add_uint64(cc_freq
* rem_value_in_ns
/ NS_PER_S_U
,
688 value_period_cycles
)) {
693 *raw_value
= cc_freq
* rem_value_in_ns
/ NS_PER_S_U
+
700 #include <babeltrace2/undef-func-status.h>
702 #endif /* BABELTRACE_COMMON_INTERNAL_H */