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"
35 #define BT_COMMON_COLOR_RESET "\033[0m"
36 #define BT_COMMON_COLOR_BOLD "\033[1m"
37 #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
38 #define BT_COMMON_COLOR_FG_RED "\033[31m"
39 #define BT_COMMON_COLOR_FG_GREEN "\033[32m"
40 #define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
41 #define BT_COMMON_COLOR_FG_BLUE "\033[34m"
42 #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
43 #define BT_COMMON_COLOR_FG_CYAN "\033[36m"
44 #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
45 #define BT_COMMON_COLOR_FG_BOLD_RED "\033[1m\033[31m"
46 #define BT_COMMON_COLOR_FG_BOLD_GREEN "\033[1m\033[32m"
47 #define BT_COMMON_COLOR_FG_BOLD_YELLOW "\033[1m\033[33m"
48 #define BT_COMMON_COLOR_FG_BOLD_BLUE "\033[1m\033[34m"
49 #define BT_COMMON_COLOR_FG_BOLD_MAGENTA "\033[1m\033[35m"
50 #define BT_COMMON_COLOR_FG_BOLD_CYAN "\033[1m\033[36m"
51 #define BT_COMMON_COLOR_FG_BOLD_LIGHT_GRAY "\033[1m\033[37m"
52 #define BT_COMMON_COLOR_FG_BRIGHT_RED "\033[91m"
53 #define BT_COMMON_COLOR_FG_BRIGHT_GREEN "\033[92m"
54 #define BT_COMMON_COLOR_FG_BRIGHT_YELLOW "\033[93m"
55 #define BT_COMMON_COLOR_FG_BRIGHT_BLUE "\033[94m"
56 #define BT_COMMON_COLOR_FG_BRIGHT_MAGENTA "\033[95m"
57 #define BT_COMMON_COLOR_FG_BRIGHT_CYAN "\033[96m"
58 #define BT_COMMON_COLOR_FG_BRIGHT_LIGHT_GRAY "\033[97m"
59 #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
60 #define BT_COMMON_COLOR_BG_RED "\033[41m"
61 #define BT_COMMON_COLOR_BG_GREEN "\033[42m"
62 #define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
63 #define BT_COMMON_COLOR_BG_BLUE "\033[44m"
64 #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
65 #define BT_COMMON_COLOR_BG_CYAN "\033[46m"
66 #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
68 enum bt_common_color_when
{
69 BT_COMMON_COLOR_WHEN_AUTO
,
70 BT_COMMON_COLOR_WHEN_ALWAYS
,
71 BT_COMMON_COLOR_WHEN_NEVER
,
74 struct bt_common_color_codes
{
77 const char *fg_default
;
80 const char *fg_yellow
;
82 const char *fg_magenta
;
84 const char *fg_light_gray
;
85 const char *fg_bright_red
;
86 const char *fg_bright_green
;
87 const char *fg_bright_yellow
;
88 const char *fg_bright_blue
;
89 const char *fg_bright_magenta
;
90 const char *fg_bright_cyan
;
91 const char *fg_bright_light_gray
;
92 const char *bg_default
;
95 const char *bg_yellow
;
97 const char *bg_magenta
;
99 const char *bg_light_gray
;
102 struct bt_common_lttng_live_url_parts
{
105 GString
*target_hostname
;
106 GString
*session_name
;
108 /* -1 means default port */
113 * Checks if the current process has setuid or setgid access rights.
114 * Returns `true` if so.
117 bool bt_common_is_setuid_setgid(void);
120 * Returns the system-wide plugin path, e.g.
121 * `/usr/lib/babeltrace2/plugins`. Do not free the return value.
124 const char *bt_common_get_system_plugin_path(void);
127 * Returns the user plugin path, e.g.
128 * `/home/user/.local/lib/babeltrace2/plugins`. You need to free the
132 char *bt_common_get_home_plugin_path(int log_level
);
135 * Appends the list of directories in `paths` to the array `dirs`.
136 * `paths` is a list of directories separated by `:`. Returns 0 on
140 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
);
143 * Returns `true` if terminal color codes are supported for this
147 bool bt_common_colors_supported(void);
150 const char *bt_common_color_reset(void);
153 const char *bt_common_color_bold(void);
156 const char *bt_common_color_fg_default(void);
159 const char *bt_common_color_fg_red(void);
162 const char *bt_common_color_fg_green(void);
165 const char *bt_common_color_fg_yellow(void);
168 const char *bt_common_color_fg_blue(void);
171 const char *bt_common_color_fg_magenta(void);
174 const char *bt_common_color_fg_cyan(void);
177 const char *bt_common_color_fg_light_gray(void);
180 const char *bt_common_color_fg_bright_red(void);
183 const char *bt_common_color_fg_bright_green(void);
186 const char *bt_common_color_fg_bright_yellow(void);
189 const char *bt_common_color_fg_bright_blue(void);
192 const char *bt_common_color_fg_bright_magenta(void);
195 const char *bt_common_color_fg_bright_cyan(void);
198 const char *bt_common_color_fg_bright_light_gray(void);
201 const char *bt_common_color_bg_default(void);
204 const char *bt_common_color_bg_red(void);
207 const char *bt_common_color_bg_green(void);
210 const char *bt_common_color_bg_yellow(void);
213 const char *bt_common_color_bg_blue(void);
216 const char *bt_common_color_bg_magenta(void);
219 const char *bt_common_color_bg_cyan(void);
222 const char *bt_common_color_bg_light_gray(void);
225 void bt_common_color_get_codes(struct bt_common_color_codes
*codes
,
226 enum bt_common_color_when use_colors
);
229 * Returns the substring from `input` to the first character found
230 * in the list of characters `end_chars`, unescaping any character
231 * found in `escapable_chars`, and sets `*end_pos` to the position of
232 * the end (from `input`). The caller owns the returned GString.
235 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
236 const char *end_chars
, size_t *end_pos
);
239 * Returns the quoted version of `input` for a shell. If
240 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
241 * and suffix to the returned string; otherwise the caller should
242 * prepend and append them manually, although they are not always
243 * required. The caller owns the returned GString.
246 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
);
249 * Returns `true` if `input` is a string made only of printable
253 bool bt_common_string_is_printable(const char *input
);
256 * Destroys the parts of an LTTng live URL as returned by
257 * bt_common_parse_lttng_live_url().
260 void bt_common_destroy_lttng_live_url_parts(
261 struct bt_common_lttng_live_url_parts
*parts
);
264 * Parses the LTTng live URL `url` and returns its different parts.
265 * If there's an error, writes the error message into `*error_buf`
266 * up to `error_buf_size` bytes. You must destroy the returned value
267 * with bt_common_destroy_lttng_live_url_parts().
270 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
271 const char *url
, char *error_buf
, size_t error_buf_size
);
274 * Normalizes (in place) a star globbing pattern to be used with
275 * bt_common_star_glob_match(). This function always succeeds.
278 void bt_common_normalize_star_glob_pattern(char *pattern
);
281 * Returns `true` if `candidate` (of size `candidate_len`) matches
282 * the star globbing pattern `pattern` (of size `pattern_len`).
285 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
286 const char *candidate
, size_t candidate_len
);
289 * Normalizes the path `path`:
291 * * If it's a relative path, converts it to an absolute path using
292 * `wd` as the working directory (or the current working directory
294 * * Removes consecutive and trailing slashes.
295 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
296 * * Does NOT resolve symbolic links.
298 * The caller owns the returned GString.
301 GString
*bt_common_normalize_path(const char *path
, const char *wd
);
303 typedef void (* bt_common_handle_custom_specifier_func
)(void *priv_data
,
304 char **buf
, size_t avail_size
, const char **fmt
, va_list *args
);
307 * This is a custom vsnprintf() which handles the standard conversion
308 * specifier as well as custom ones.
310 * `fmt` is a typical printf()-style format string, with the following
313 * * The `*` width specifier is not accepted.
314 * * The `*` precision specifier is not accepted.
315 * * The `j` and `t` length modifiers are not accepted.
316 * * The `n` format specifier is not accepted.
317 * * The format specifiers defined in <inttypes.h> are not accepted
318 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
321 * `intro` specifies which special character immediately following an
322 * introductory `%` character in `fmt` is used to indicate a custom
323 * conversion specifier. For example, if `intro` is '@', then any `%@`
324 * sequence in `fmt` is the beginning of a custom conversion specifier.
326 * When a custom conversion specifier is encountered in `fmt`,
327 * the function calls `handle_specifier`. This callback receives:
330 * Custom, private data.
333 * Address of the current buffer pointer. `*buf` is the position to
334 * append new data. The callback must update `*buf` when appending
335 * new data. The callback must ensure not to write passed the whole
336 * buffer passed to bt_common_custom_vsnprintf().
339 * Number of bytes left in whole buffer from the `*buf` point.
342 * Address of the current format string pointer. `*fmt` points to
343 * the introductory `%` character, which is followed by the
344 * character `intro`. The callback must update `*fmt` so that it
345 * points after the whole custom conversion specifier.
348 * Variable argument list. Use va_arg() to get new arguments from
349 * this list and update it at the same time.
351 * Because this is an internal utility, this function and its callback
352 * do not return error codes: they abort when there's any error (bad
353 * format string, for example).
356 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
358 bt_common_handle_custom_specifier_func handle_specifier
,
359 void *priv_data
, const char *fmt
, va_list *args
);
362 * Variadic form of bt_common_custom_vsnprintf().
365 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
367 bt_common_handle_custom_specifier_func handle_specifier
,
368 void *priv_data
, const char *fmt
, ...);
371 * Returns the system page size.
374 size_t bt_common_get_page_size(int log_level
);
377 * Adds the digit separator `sep` as many times as needed to form groups
378 * of `digits_per_group` digits within `str`. `str` must have enough
379 * room to accomodate the new separators, that is:
381 * strlen(str) + (strlen(str) / digits_per_group) + 1
383 * Example: with `str` `1983198398213`, `digits_per_group` 3, and `sep`
384 * `,`, `str` becomes `1,983,198,398,213`.
386 * `strlen(str)` must not be 0. `digits_per_group` must not be 0. `sep`
390 void bt_common_sep_digits(char *str
, unsigned int digits_per_group
, char sep
);
393 * This is similar to what the command `fold --spaces` does: it wraps
394 * the input lines of `str`, breaking at spaces, and indenting each line
395 * with `indent` spaces so that each line fits the total length
398 * If an original line in `str` contains a word which is >= the content
399 * length (`total_length - indent`), then the corresponding folded line
400 * is also larger than the content length. In other words, breaking at
401 * spaces is a best effort, but it might not be possible.
403 * The returned string, on success, is owned by the caller.
406 GString
*bt_common_fold(const char *str
, unsigned int total_length
,
407 unsigned int indent
);
410 * Writes the terminal's width to `*width`, its height to `*height`,
411 * and returns 0 on success, or returns -1 on error.
414 int bt_common_get_term_size(unsigned int *width
, unsigned int *height
);
417 * Appends the textual content of `fp` to `str`, starting from its
418 * current position to the end of the file.
420 * This function does NOT rewind `fp` once it's done or on error.
423 int bt_common_append_file_content_to_g_string(GString
*str
, FILE *fp
);
426 void bt_common_abort(void) __attribute__((noreturn
));
429 * Wraps read() function to handle EINTR and partial reads.
430 * On success, it returns `count` received as parameter. On error, it returns a
431 * value smaller than the requested `count`.
434 ssize_t
bt_common_read(int fd
, void *buf
, size_t count
, int log_level
)
441 /* Never return an overflow value. */
442 BT_ASSERT_DBG(count
<= SSIZE_MAX
);
445 ret
= read(fd
, buf
+ i
, count
- i
);
447 if (errno
== EINTR
) {
448 #ifdef BT_LOG_WRITE_CUR_LVL
449 BT_LOG_WRITE_CUR_LVL(BT_LOG_DEBUG
, log_level
,
451 "read() call interrupted; retrying...");
453 /* retry operation */
456 #ifdef BT_LOG_WRITE_ERRNO_CUR_LVL
457 BT_LOG_WRITE_ERRNO_CUR_LVL(BT_LOG_ERROR
,
458 log_level
, BT_LOG_TAG
,
459 "Error while reading", ": fd=%d", fd
);
465 BT_ASSERT_DBG(i
<= count
);
466 } while (count
- i
> 0 && ret
> 0);
481 const char *bt_common_field_class_type_string(enum bt_field_class_type class_type
)
483 switch (class_type
) {
484 case BT_FIELD_CLASS_TYPE_BOOL
:
486 case BT_FIELD_CLASS_TYPE_BIT_ARRAY
:
488 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
489 return "UNSIGNED_INTEGER";
490 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
491 return "SIGNED_INTEGER";
492 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
493 return "UNSIGNED_ENUMERATION";
494 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
495 return "SIGNED_ENUMERATION";
496 case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL
:
497 return "SINGLE_PRECISION_REAL";
498 case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL
:
499 return "DOUBLE_PRECISION_REAL";
500 case BT_FIELD_CLASS_TYPE_STRING
:
502 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
504 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
505 return "STATIC_ARRAY";
506 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD
:
507 return "DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD";
508 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD
:
509 return "DYNAMIC_ARRAY_WITH_LENGTH_FIELD";
510 case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD
:
511 return "OPTION_WITHOUT_SELECTOR_FIELD";
512 case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD
:
513 return "OPTION_WITH_BOOL_SELECTOR_FIELD";
514 case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
:
515 return "OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD";
516 case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD
:
517 return "OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD";
518 case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD
:
519 return "VARIANT_WITHOUT_SELECTOR_FIELD";
520 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD
:
521 return "VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD";
522 case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD
:
523 return "VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD";
530 const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base
)
533 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
535 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
537 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
539 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
540 return "HEXADECIMAL";
547 const char *bt_common_scope_string(enum bt_field_path_scope scope
)
550 case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT
:
551 return "PACKET_CONTEXT";
552 case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT
:
553 return "EVENT_COMMON_CONTEXT";
554 case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT
:
555 return "EVENT_SPECIFIC_CONTEXT";
556 case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD
:
557 return "EVENT_PAYLOAD";
564 const char *bt_common_event_class_log_level_string(
565 enum bt_event_class_log_level level
)
568 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
:
570 case BT_EVENT_CLASS_LOG_LEVEL_ALERT
:
572 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
:
574 case BT_EVENT_CLASS_LOG_LEVEL_ERROR
:
576 case BT_EVENT_CLASS_LOG_LEVEL_WARNING
:
578 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE
:
580 case BT_EVENT_CLASS_LOG_LEVEL_INFO
:
582 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
:
583 return "DEBUG_SYSTEM";
584 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
:
585 return "DEBUG_PROGRAM";
586 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
:
587 return "DEBUG_PROCESS";
588 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
:
589 return "DEBUG_MODULE";
590 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
:
592 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
:
593 return "DEBUG_FUNCTION";
594 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
:
596 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG
:
604 const char *bt_common_value_type_string(enum bt_value_type type
)
607 case BT_VALUE_TYPE_NULL
:
609 case BT_VALUE_TYPE_BOOL
:
611 case BT_VALUE_TYPE_UNSIGNED_INTEGER
:
612 return "UNSIGNED_INTEGER";
613 case BT_VALUE_TYPE_SIGNED_INTEGER
:
614 return "SIGNED_INTEGER";
615 case BT_VALUE_TYPE_REAL
:
617 case BT_VALUE_TYPE_STRING
:
619 case BT_VALUE_TYPE_ARRAY
:
621 case BT_VALUE_TYPE_MAP
:
629 GString
*bt_common_field_path_string(struct bt_field_path
*path
)
631 GString
*str
= g_string_new(NULL
);
640 g_string_append_printf(str
, "[%s", bt_common_scope_string(
641 bt_field_path_get_root_scope(path
)));
643 for (i
= 0; i
< bt_field_path_get_item_count(path
); i
++) {
644 const struct bt_field_path_item
*fp_item
=
645 bt_field_path_borrow_item_by_index_const(path
, i
);
647 switch (bt_field_path_item_get_type(fp_item
)) {
648 case BT_FIELD_PATH_ITEM_TYPE_INDEX
:
649 g_string_append_printf(str
, ", %" PRIu64
,
650 bt_field_path_item_index_get_index(fp_item
));
652 case BT_FIELD_PATH_ITEM_TYPE_CURRENT_ARRAY_ELEMENT
:
653 g_string_append(str
, ", <CUR>");
660 g_string_append(str
, "]");
667 const char *bt_common_logging_level_string(
668 enum bt_logging_level level
)
671 case BT_LOGGING_LEVEL_TRACE
:
673 case BT_LOGGING_LEVEL_DEBUG
:
675 case BT_LOGGING_LEVEL_INFO
:
677 case BT_LOGGING_LEVEL_WARNING
:
679 case BT_LOGGING_LEVEL_ERROR
:
681 case BT_LOGGING_LEVEL_FATAL
:
683 case BT_LOGGING_LEVEL_NONE
:
691 const char *bt_common_func_status_string(int status
)
694 case __BT_FUNC_STATUS_OVERFLOW_ERROR
:
696 case __BT_FUNC_STATUS_UNKNOWN_OBJECT
:
697 return "UNKNOWN_OBJECT";
698 case __BT_FUNC_STATUS_MEMORY_ERROR
:
699 return "MEMORY_ERROR";
700 case __BT_FUNC_STATUS_USER_ERROR
:
702 case __BT_FUNC_STATUS_ERROR
:
704 case __BT_FUNC_STATUS_OK
:
706 case __BT_FUNC_STATUS_END
:
708 case __BT_FUNC_STATUS_NOT_FOUND
:
710 case __BT_FUNC_STATUS_AGAIN
:
712 case __BT_FUNC_STATUS_INTERRUPTED
:
713 return "INTERRUPTED";
719 #define NS_PER_S_I INT64_C(1000000000)
720 #define NS_PER_S_U UINT64_C(1000000000)
723 int bt_common_clock_value_from_ns_from_origin(
724 int64_t cc_offset_seconds
, uint64_t cc_offset_cycles
,
725 uint64_t cc_freq
, int64_t ns_from_origin
,
729 int64_t offset_in_ns
;
730 uint64_t value_in_ns
;
731 uint64_t rem_value_in_ns
;
732 uint64_t value_periods
;
733 uint64_t value_period_cycles
;
736 BT_ASSERT_DBG(raw_value
);
738 /* Compute offset part of requested value, in nanoseconds */
739 if (!bt_safe_to_mul_int64(cc_offset_seconds
, NS_PER_S_I
)) {
744 offset_in_ns
= cc_offset_seconds
* NS_PER_S_I
;
746 if (cc_freq
== NS_PER_S_U
) {
747 ns_to_add
= (int64_t) cc_offset_cycles
;
749 if (!bt_safe_to_mul_int64((int64_t) cc_offset_cycles
,
755 ns_to_add
= ((int64_t) cc_offset_cycles
* NS_PER_S_I
) /
759 if (!bt_safe_to_add_int64(offset_in_ns
, ns_to_add
)) {
764 offset_in_ns
+= ns_to_add
;
766 /* Value part in nanoseconds */
767 if (ns_from_origin
< offset_in_ns
) {
772 value_in_ns
= (uint64_t) (ns_from_origin
- offset_in_ns
);
774 /* Number of whole clock periods in `value_in_ns` */
775 value_periods
= value_in_ns
/ NS_PER_S_U
;
777 /* Remaining nanoseconds in cycles + whole clock periods in cycles */
778 rem_value_in_ns
= value_in_ns
- value_periods
* NS_PER_S_U
;
780 if (value_periods
> UINT64_MAX
/ cc_freq
) {
785 if (!bt_safe_to_mul_uint64(value_periods
, cc_freq
)) {
790 value_period_cycles
= value_periods
* cc_freq
;
792 if (!bt_safe_to_mul_uint64(cc_freq
, rem_value_in_ns
)) {
797 if (!bt_safe_to_add_uint64(cc_freq
* rem_value_in_ns
/ NS_PER_S_U
,
798 value_period_cycles
)) {
803 *raw_value
= cc_freq
* rem_value_in_ns
/ NS_PER_S_U
+
811 * bt_g_string_append_printf cannot be inlined because it expects a
812 * variadic argument list.
814 BT_HIDDEN
__BT_ATTR_FORMAT_PRINTF(2, 3)
815 int bt_common_g_string_append_printf(GString
*str
, const char *fmt
, ...);
818 void bt_common_g_string_append(GString
*str
, const char *s
)
820 gsize len
, allocated_len
, s_len
;
822 /* str->len excludes \0. */
825 allocated_len
= str
->allocated_len
- 1;
827 if (G_UNLIKELY(allocated_len
< len
+ s_len
)) {
829 g_string_set_size(str
, len
+ s_len
);
831 str
->len
= len
+ s_len
;
833 memcpy(str
->str
+ len
, s
, s_len
+ 1);
837 void bt_common_g_string_append_c(GString
*str
, char c
)
839 gsize len
, allocated_len
, s_len
;
841 /* str->len excludes \0. */
844 allocated_len
= str
->allocated_len
- 1;
846 if (G_UNLIKELY(allocated_len
< len
+ s_len
)) {
848 g_string_set_size(str
, len
+ s_len
);
850 str
->len
= len
+ s_len
;
853 str
->str
[len
+ 1] = '\0';
860 #endif /* BABELTRACE_COMMON_INTERNAL_H */