2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
8 #ifndef BABELTRACE_COMMON_INTERNAL_H
9 #define BABELTRACE_COMMON_INTERNAL_H
22 #include <babeltrace2/babeltrace.h>
24 #define __BT_IN_BABELTRACE_H
25 #include <babeltrace2/func-status.h>
27 #include "common/assert.h"
28 #include "common/macros.h"
29 #include "common/safe.h"
31 #define BT_COMMON_COLOR_RESET "\033[0m"
32 #define BT_COMMON_COLOR_BOLD "\033[1m"
33 #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
34 #define BT_COMMON_COLOR_FG_RED "\033[31m"
35 #define BT_COMMON_COLOR_FG_GREEN "\033[32m"
36 #define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
37 #define BT_COMMON_COLOR_FG_BLUE "\033[34m"
38 #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
39 #define BT_COMMON_COLOR_FG_CYAN "\033[36m"
40 #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
41 #define BT_COMMON_COLOR_FG_BOLD_RED "\033[1m\033[31m"
42 #define BT_COMMON_COLOR_FG_BOLD_GREEN "\033[1m\033[32m"
43 #define BT_COMMON_COLOR_FG_BOLD_YELLOW "\033[1m\033[33m"
44 #define BT_COMMON_COLOR_FG_BOLD_BLUE "\033[1m\033[34m"
45 #define BT_COMMON_COLOR_FG_BOLD_MAGENTA "\033[1m\033[35m"
46 #define BT_COMMON_COLOR_FG_BOLD_CYAN "\033[1m\033[36m"
47 #define BT_COMMON_COLOR_FG_BOLD_LIGHT_GRAY "\033[1m\033[37m"
48 #define BT_COMMON_COLOR_FG_BRIGHT_RED "\033[91m"
49 #define BT_COMMON_COLOR_FG_BRIGHT_GREEN "\033[92m"
50 #define BT_COMMON_COLOR_FG_BRIGHT_YELLOW "\033[93m"
51 #define BT_COMMON_COLOR_FG_BRIGHT_BLUE "\033[94m"
52 #define BT_COMMON_COLOR_FG_BRIGHT_MAGENTA "\033[95m"
53 #define BT_COMMON_COLOR_FG_BRIGHT_CYAN "\033[96m"
54 #define BT_COMMON_COLOR_FG_BRIGHT_LIGHT_GRAY "\033[97m"
55 #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
56 #define BT_COMMON_COLOR_BG_RED "\033[41m"
57 #define BT_COMMON_COLOR_BG_GREEN "\033[42m"
58 #define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
59 #define BT_COMMON_COLOR_BG_BLUE "\033[44m"
60 #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
61 #define BT_COMMON_COLOR_BG_CYAN "\033[46m"
62 #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
64 enum bt_common_color_when
{
65 BT_COMMON_COLOR_WHEN_AUTO
,
66 BT_COMMON_COLOR_WHEN_ALWAYS
,
67 BT_COMMON_COLOR_WHEN_NEVER
,
70 struct bt_common_color_codes
{
73 const char *fg_default
;
76 const char *fg_yellow
;
78 const char *fg_magenta
;
80 const char *fg_light_gray
;
81 const char *fg_bright_red
;
82 const char *fg_bright_green
;
83 const char *fg_bright_yellow
;
84 const char *fg_bright_blue
;
85 const char *fg_bright_magenta
;
86 const char *fg_bright_cyan
;
87 const char *fg_bright_light_gray
;
88 const char *bg_default
;
91 const char *bg_yellow
;
93 const char *bg_magenta
;
95 const char *bg_light_gray
;
98 struct bt_common_lttng_live_url_parts
{
101 GString
*target_hostname
;
102 GString
*session_name
;
104 /* -1 means default port */
109 * Checks if the current process has setuid or setgid access rights.
110 * Returns `true` if so.
113 bool bt_common_is_setuid_setgid(void);
116 * Returns the system-wide plugin path, e.g.
117 * `/usr/lib/babeltrace2/plugins`. Do not free the return value.
120 const char *bt_common_get_system_plugin_path(void);
123 * Returns the user plugin path, e.g.
124 * `/home/user/.local/lib/babeltrace2/plugins`. You need to free the
128 char *bt_common_get_home_plugin_path(int log_level
);
131 * Appends the list of directories in `paths` to the array `dirs`.
132 * `paths` is a list of directories separated by `:`. Returns 0 on
136 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
);
139 * Returns `true` if terminal color codes are supported for this
143 bool bt_common_colors_supported(void);
146 const char *bt_common_color_reset(void);
149 const char *bt_common_color_bold(void);
152 const char *bt_common_color_fg_default(void);
155 const char *bt_common_color_fg_red(void);
158 const char *bt_common_color_fg_green(void);
161 const char *bt_common_color_fg_yellow(void);
164 const char *bt_common_color_fg_blue(void);
167 const char *bt_common_color_fg_magenta(void);
170 const char *bt_common_color_fg_cyan(void);
173 const char *bt_common_color_fg_light_gray(void);
176 const char *bt_common_color_fg_bright_red(void);
179 const char *bt_common_color_fg_bright_green(void);
182 const char *bt_common_color_fg_bright_yellow(void);
185 const char *bt_common_color_fg_bright_blue(void);
188 const char *bt_common_color_fg_bright_magenta(void);
191 const char *bt_common_color_fg_bright_cyan(void);
194 const char *bt_common_color_fg_bright_light_gray(void);
197 const char *bt_common_color_bg_default(void);
200 const char *bt_common_color_bg_red(void);
203 const char *bt_common_color_bg_green(void);
206 const char *bt_common_color_bg_yellow(void);
209 const char *bt_common_color_bg_blue(void);
212 const char *bt_common_color_bg_magenta(void);
215 const char *bt_common_color_bg_cyan(void);
218 const char *bt_common_color_bg_light_gray(void);
221 void bt_common_color_get_codes(struct bt_common_color_codes
*codes
,
222 enum bt_common_color_when use_colors
);
225 * Returns the substring from `input` to the first character found
226 * in the list of characters `end_chars`, unescaping any character
227 * found in `escapable_chars`, and sets `*end_pos` to the position of
228 * the end (from `input`). The caller owns the returned GString.
231 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
232 const char *end_chars
, size_t *end_pos
);
235 * Returns the quoted version of `input` for a shell. If
236 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
237 * and suffix to the returned string; otherwise the caller should
238 * prepend and append them manually, although they are not always
239 * required. The caller owns the returned GString.
242 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
);
245 * Returns `true` if `input` is a string made only of printable
249 bool bt_common_string_is_printable(const char *input
);
252 * Destroys the parts of an LTTng live URL as returned by
253 * bt_common_parse_lttng_live_url().
256 void bt_common_destroy_lttng_live_url_parts(
257 struct bt_common_lttng_live_url_parts
*parts
);
260 * Parses the LTTng live URL `url` and returns its different parts.
261 * If there's an error, writes the error message into `*error_buf`
262 * up to `error_buf_size` bytes. You must destroy the returned value
263 * with bt_common_destroy_lttng_live_url_parts().
266 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
267 const char *url
, char *error_buf
, size_t error_buf_size
);
270 * Normalizes (in place) a star globbing pattern to be used with
271 * bt_common_star_glob_match(). This function always succeeds.
274 void bt_common_normalize_star_glob_pattern(char *pattern
);
277 * Returns `true` if `candidate` (of size `candidate_len`) matches
278 * the star globbing pattern `pattern` (of size `pattern_len`).
281 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
282 const char *candidate
, size_t candidate_len
);
285 * Normalizes the path `path`:
287 * * If it's a relative path, converts it to an absolute path using
288 * `wd` as the working directory (or the current working directory
290 * * Removes consecutive and trailing slashes.
291 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
292 * * Does NOT resolve symbolic links.
294 * The caller owns the returned GString.
297 GString
*bt_common_normalize_path(const char *path
, const char *wd
);
299 typedef void (* bt_common_handle_custom_specifier_func
)(void *priv_data
,
300 char **buf
, size_t avail_size
, const char **fmt
, va_list *args
);
303 * This is a custom vsnprintf() which handles the standard conversion
304 * specifier as well as custom ones.
306 * `fmt` is a typical printf()-style format string, with the following
309 * * The `*` width specifier is not accepted.
310 * * The `*` precision specifier is not accepted.
311 * * The `j` and `t` length modifiers are not accepted.
312 * * The `n` format specifier is not accepted.
313 * * The format specifiers defined in <inttypes.h> are not accepted
314 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
317 * `intro` specifies which special character immediately following an
318 * introductory `%` character in `fmt` is used to indicate a custom
319 * conversion specifier. For example, if `intro` is '@', then any `%@`
320 * sequence in `fmt` is the beginning of a custom conversion specifier.
322 * When a custom conversion specifier is encountered in `fmt`,
323 * the function calls `handle_specifier`. This callback receives:
326 * Custom, private data.
329 * Address of the current buffer pointer. `*buf` is the position to
330 * append new data. The callback must update `*buf` when appending
331 * new data. The callback must ensure not to write passed the whole
332 * buffer passed to bt_common_custom_vsnprintf().
335 * Number of bytes left in whole buffer from the `*buf` point.
338 * Address of the current format string pointer. `*fmt` points to
339 * the introductory `%` character, which is followed by the
340 * character `intro`. The callback must update `*fmt` so that it
341 * points after the whole custom conversion specifier.
344 * Variable argument list. Use va_arg() to get new arguments from
345 * this list and update it at the same time.
347 * Because this is an internal utility, this function and its callback
348 * do not return error codes: they abort when there's any error (bad
349 * format string, for example).
352 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
354 bt_common_handle_custom_specifier_func handle_specifier
,
355 void *priv_data
, const char *fmt
, va_list *args
);
358 * Variadic form of bt_common_custom_vsnprintf().
361 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
363 bt_common_handle_custom_specifier_func handle_specifier
,
364 void *priv_data
, const char *fmt
, ...);
367 * Returns the system page size.
370 size_t bt_common_get_page_size(int log_level
);
373 * Adds the digit separator `sep` as many times as needed to form groups
374 * of `digits_per_group` digits within `str`. `str` must have enough
375 * room to accomodate the new separators, that is:
377 * strlen(str) + (strlen(str) / digits_per_group) + 1
379 * Example: with `str` `1983198398213`, `digits_per_group` 3, and `sep`
380 * `,`, `str` becomes `1,983,198,398,213`.
382 * `strlen(str)` must not be 0. `digits_per_group` must not be 0. `sep`
386 void bt_common_sep_digits(char *str
, unsigned int digits_per_group
, char sep
);
389 * This is similar to what the command `fold --spaces` does: it wraps
390 * the input lines of `str`, breaking at spaces, and indenting each line
391 * with `indent` spaces so that each line fits the total length
394 * If an original line in `str` contains a word which is >= the content
395 * length (`total_length - indent`), then the corresponding folded line
396 * is also larger than the content length. In other words, breaking at
397 * spaces is a best effort, but it might not be possible.
399 * The returned string, on success, is owned by the caller.
402 GString
*bt_common_fold(const char *str
, unsigned int total_length
,
403 unsigned int indent
);
406 * Writes the terminal's width to `*width`, its height to `*height`,
407 * and returns 0 on success, or returns -1 on error.
410 int bt_common_get_term_size(unsigned int *width
, unsigned int *height
);
413 * Appends the textual content of `fp` to `str`, starting from its
414 * current position to the end of the file.
416 * This function does NOT rewind `fp` once it's done or on error.
419 int bt_common_append_file_content_to_g_string(GString
*str
, FILE *fp
);
422 void bt_common_abort(void) __attribute__((noreturn
));
425 * Wraps read() function to handle EINTR and partial reads.
426 * On success, it returns `count` received as parameter. On error, it returns a
427 * value smaller than the requested `count`.
430 ssize_t
bt_common_read(int fd
, void *buf
, size_t count
, int log_level
)
437 /* Never return an overflow value. */
438 BT_ASSERT_DBG(count
<= SSIZE_MAX
);
441 ret
= read(fd
, buf
+ i
, count
- i
);
443 if (errno
== EINTR
) {
444 #ifdef BT_LOG_WRITE_CUR_LVL
445 BT_LOG_WRITE_CUR_LVL(BT_LOG_DEBUG
, log_level
,
447 "read() call interrupted; retrying...");
449 /* retry operation */
452 #ifdef BT_LOG_WRITE_ERRNO_CUR_LVL
453 BT_LOG_WRITE_ERRNO_CUR_LVL(BT_LOG_ERROR
,
454 log_level
, BT_LOG_TAG
,
455 "Error while reading", ": fd=%d", fd
);
461 BT_ASSERT_DBG(i
<= count
);
462 } while (count
- i
> 0 && ret
> 0);
477 const char *bt_common_field_class_type_string(enum bt_field_class_type class_type
)
479 switch (class_type
) {
480 case BT_FIELD_CLASS_TYPE_BOOL
:
482 case BT_FIELD_CLASS_TYPE_BIT_ARRAY
:
484 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
485 return "UNSIGNED_INTEGER";
486 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
487 return "SIGNED_INTEGER";
488 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
489 return "UNSIGNED_ENUMERATION";
490 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
491 return "SIGNED_ENUMERATION";
492 case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
:
493 return "SINGLE_PRECISION_REAL";
494 case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
:
495 return "DOUBLE_PRECISION_REAL";
496 case BT_FIELD_CLASS_TYPE_STRING
:
498 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
500 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
501 return "STATIC_ARRAY";
502 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
:
503 return "DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD";
504 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
:
505 return "DYNAMIC_ARRAY_WITH_LENGTH_FIELD";
506 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
:
507 return "OPTION_WITHOUT_SELECTOR_FIELD";
508 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
:
509 return "OPTION_WITH_BOOL_SELECTOR_FIELD";
510 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
:
511 return "OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD";
512 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
:
513 return "OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD";
514 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
:
515 return "VARIANT_WITHOUT_SELECTOR_FIELD";
516 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
:
517 return "VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD";
518 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
:
519 return "VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD";
526 const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base
)
529 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
531 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
533 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
535 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
536 return "HEXADECIMAL";
543 const char *bt_common_scope_string(enum bt_field_path_scope scope
)
546 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
:
547 return "PACKET_CONTEXT";
548 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
:
549 return "EVENT_COMMON_CONTEXT";
550 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
:
551 return "EVENT_SPECIFIC_CONTEXT";
552 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
:
553 return "EVENT_PAYLOAD";
560 const char *bt_common_event_class_log_level_string(
561 enum bt_event_class_log_level level
)
564 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
:
566 case BT_EVENT_CLASS_LOG_LEVEL_ALERT
:
568 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
:
570 case BT_EVENT_CLASS_LOG_LEVEL_ERROR
:
572 case BT_EVENT_CLASS_LOG_LEVEL_WARNING
:
574 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE
:
576 case BT_EVENT_CLASS_LOG_LEVEL_INFO
:
578 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
:
579 return "DEBUG_SYSTEM";
580 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
:
581 return "DEBUG_PROGRAM";
582 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
:
583 return "DEBUG_PROCESS";
584 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
:
585 return "DEBUG_MODULE";
586 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
:
588 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
:
589 return "DEBUG_FUNCTION";
590 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
:
592 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG
:
600 const char *bt_common_value_type_string(enum bt_value_type type
)
603 case BT_VALUE_TYPE_NULL
:
605 case BT_VALUE_TYPE_BOOL
:
607 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
608 return "UNSIGNED_INTEGER";
609 case BT_VALUE_TYPE_SIGNED_INTEGER
:
610 return "SIGNED_INTEGER";
611 case BT_VALUE_TYPE_REAL
:
613 case BT_VALUE_TYPE_STRING
:
615 case BT_VALUE_TYPE_ARRAY
:
617 case BT_VALUE_TYPE_MAP
:
625 GString
*bt_common_field_path_string(struct bt_field_path
*path
)
627 GString
*str
= g_string_new(NULL
);
636 g_string_append_printf(str
, "[%s", bt_common_scope_string(
637 bt_field_path_get_root_scope(path
)));
639 for (i
= 0; i
< bt_field_path_get_item_count(path
); i
++) {
640 const struct bt_field_path_item
*fp_item
=
641 bt_field_path_borrow_item_by_index_const(path
, i
);
643 switch (bt_field_path_item_get_type(fp_item
)) {
644 case BT_FIELD_PATH_ITEM_TYPE_INDEX
:
645 g_string_append_printf(str
, ", %" PRIu64
,
646 bt_field_path_item_index_get_index(fp_item
));
648 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
:
649 g_string_append(str
, ", <CUR>");
656 g_string_append(str
, "]");
663 const char *bt_common_logging_level_string(
664 enum bt_logging_level level
)
667 case BT_LOGGING_LEVEL_TRACE
:
669 case BT_LOGGING_LEVEL_DEBUG
:
671 case BT_LOGGING_LEVEL_INFO
:
673 case BT_LOGGING_LEVEL_WARNING
:
675 case BT_LOGGING_LEVEL_ERROR
:
677 case BT_LOGGING_LEVEL_FATAL
:
679 case BT_LOGGING_LEVEL_NONE
:
687 const char *bt_common_func_status_string(int status
)
690 case __BT_FUNC_STATUS_OVERFLOW_ERROR
:
692 case __BT_FUNC_STATUS_UNKNOWN_OBJECT
:
693 return "UNKNOWN_OBJECT";
694 case __BT_FUNC_STATUS_MEMORY_ERROR
:
695 return "MEMORY_ERROR";
696 case __BT_FUNC_STATUS_USER_ERROR
:
698 case __BT_FUNC_STATUS_ERROR
:
700 case __BT_FUNC_STATUS_OK
:
702 case __BT_FUNC_STATUS_END
:
704 case __BT_FUNC_STATUS_NOT_FOUND
:
706 case __BT_FUNC_STATUS_AGAIN
:
708 case __BT_FUNC_STATUS_INTERRUPTED
:
709 return "INTERRUPTED";
715 #define NS_PER_S_I INT64_C(1000000000)
716 #define NS_PER_S_U UINT64_C(1000000000)
719 int bt_common_clock_value_from_ns_from_origin(
720 int64_t cc_offset_seconds
, uint64_t cc_offset_cycles
,
721 uint64_t cc_freq
, int64_t ns_from_origin
,
725 int64_t offset_in_ns
;
726 uint64_t value_in_ns
;
727 uint64_t rem_value_in_ns
;
728 uint64_t value_periods
;
729 uint64_t value_period_cycles
;
732 BT_ASSERT_DBG(raw_value
);
734 /* Compute offset part of requested value, in nanoseconds */
735 if (!bt_safe_to_mul_int64(cc_offset_seconds
, NS_PER_S_I
)) {
740 offset_in_ns
= cc_offset_seconds
* NS_PER_S_I
;
742 if (cc_freq
== NS_PER_S_U
) {
743 ns_to_add
= (int64_t) cc_offset_cycles
;
745 if (!bt_safe_to_mul_int64((int64_t) cc_offset_cycles
,
751 ns_to_add
= ((int64_t) cc_offset_cycles
* NS_PER_S_I
) /
755 if (!bt_safe_to_add_int64(offset_in_ns
, ns_to_add
)) {
760 offset_in_ns
+= ns_to_add
;
762 /* Value part in nanoseconds */
763 if (ns_from_origin
< offset_in_ns
) {
768 value_in_ns
= (uint64_t) (ns_from_origin
- offset_in_ns
);
770 /* Number of whole clock periods in `value_in_ns` */
771 value_periods
= value_in_ns
/ NS_PER_S_U
;
773 /* Remaining nanoseconds in cycles + whole clock periods in cycles */
774 rem_value_in_ns
= value_in_ns
- value_periods
* NS_PER_S_U
;
776 if (value_periods
> UINT64_MAX
/ cc_freq
) {
781 if (!bt_safe_to_mul_uint64(value_periods
, cc_freq
)) {
786 value_period_cycles
= value_periods
* cc_freq
;
788 if (!bt_safe_to_mul_uint64(cc_freq
, rem_value_in_ns
)) {
793 if (!bt_safe_to_add_uint64(cc_freq
* rem_value_in_ns
/ NS_PER_S_U
,
794 value_period_cycles
)) {
799 *raw_value
= cc_freq
* rem_value_in_ns
/ NS_PER_S_U
+
807 * bt_g_string_append_printf cannot be inlined because it expects a
808 * variadic argument list.
810 BT_HIDDEN
__BT_ATTR_FORMAT_PRINTF(2, 3)
811 int bt_common_g_string_append_printf(GString
*str
, const char *fmt
, ...);
814 void bt_common_g_string_append(GString
*str
, const char *s
)
816 gsize len
, allocated_len
, s_len
;
818 /* str->len excludes \0. */
821 allocated_len
= str
->allocated_len
- 1;
823 if (G_UNLIKELY(allocated_len
< len
+ s_len
)) {
825 g_string_set_size(str
, len
+ s_len
);
827 str
->len
= len
+ s_len
;
829 memcpy(str
->str
+ len
, s
, s_len
+ 1);
833 void bt_common_g_string_append_c(GString
*str
, char c
)
835 gsize len
, allocated_len
, s_len
;
837 /* str->len excludes \0. */
840 allocated_len
= str
->allocated_len
- 1;
842 if (G_UNLIKELY(allocated_len
< len
+ s_len
)) {
844 g_string_set_size(str
, len
+ s_len
);
846 str
->len
= len
+ s_len
;
849 str
->str
[len
+ 1] = '\0';
852 #endif /* BABELTRACE_COMMON_INTERNAL_H */