2 * SPDX-License-Identifier: MIT
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 * Babeltrace common functions
9 #define BT_LOG_OUTPUT_LEVEL log_level
10 #define BT_LOG_TAG "COMMON"
11 #include "logging/log.h"
16 #include <sys/types.h>
19 #include "common/assert.h"
27 #include "common/macros.h"
28 #include "common/common.h"
29 #include "compat/unistd.h"
30 #include "compat/limits.h"
34 #include <sys/ioctl.h>
37 #define SYSTEM_PLUGIN_PATH BABELTRACE_PLUGINS_DIR
38 #define HOME_ENV_VAR "HOME"
39 #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace2/plugins"
41 static const char *bt_common_color_code_reset
= "";
42 static const char *bt_common_color_code_bold
= "";
43 static const char *bt_common_color_code_fg_default
= "";
44 static const char *bt_common_color_code_fg_red
= "";
45 static const char *bt_common_color_code_fg_green
= "";
46 static const char *bt_common_color_code_fg_yellow
= "";
47 static const char *bt_common_color_code_fg_blue
= "";
48 static const char *bt_common_color_code_fg_magenta
= "";
49 static const char *bt_common_color_code_fg_cyan
= "";
50 static const char *bt_common_color_code_fg_light_gray
= "";
51 static const char *bt_common_color_code_fg_bright_red
= "";
52 static const char *bt_common_color_code_fg_bright_green
= "";
53 static const char *bt_common_color_code_fg_bright_yellow
= "";
54 static const char *bt_common_color_code_fg_bright_blue
= "";
55 static const char *bt_common_color_code_fg_bright_magenta
= "";
56 static const char *bt_common_color_code_fg_bright_cyan
= "";
57 static const char *bt_common_color_code_fg_bright_light_gray
= "";
58 static const char *bt_common_color_code_bg_default
= "";
59 static const char *bt_common_color_code_bg_red
= "";
60 static const char *bt_common_color_code_bg_green
= "";
61 static const char *bt_common_color_code_bg_yellow
= "";
62 static const char *bt_common_color_code_bg_blue
= "";
63 static const char *bt_common_color_code_bg_magenta
= "";
64 static const char *bt_common_color_code_bg_cyan
= "";
65 static const char *bt_common_color_code_bg_light_gray
= "";
68 * A color codes structure always filled with the proper color codes for the
71 static struct bt_common_color_codes color_codes
;
74 * A color codes structure always filled with empty strings, for when we want no
77 static struct bt_common_color_codes no_color_codes
= {
78 "", "", "", "", "", "", "", "", "", "",
79 "", "", "", "", "", "", "", "", "", "",
84 void __attribute__((constructor
)) bt_common_color_ctor(void)
86 const char *term_env_var
;
87 const char *bright_means_bold_env_var
;
88 bool bright_means_bold
= true;
89 const char *code_fg_bright_red
;
90 const char *code_fg_bright_green
;
91 const char *code_fg_bright_yellow
;
92 const char *code_fg_bright_blue
;
93 const char *code_fg_bright_magenta
;
94 const char *code_fg_bright_cyan
;
95 const char *code_fg_bright_light_gray
;
98 * Check whether or not the terminal supports having
99 * bold foreground colors which do _not_ become bright
100 * colors, that is, the lines
102 * $ echo -e "\033[31mTHIS\n\033[1mTHAT\033[0m"
104 * have the _same_ color, but `THAT` uses a bold font.
106 * This is the case of the kitty terminal emulator.
108 * It's also possible with GNOME Terminal since 3.27.2
109 * and xfce4-terminal since 0.8.7 (and GNOME VTE since
110 * 0.51.2), but it's user-configurable. Since we don't
111 * have this configuration value here, assume it's not
112 * the case to support old versions of GNOME Terminal.
114 * Any user can set the
115 * `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` environment
116 * variable to `0` to use the bright foreground color
117 * codes instead of making the normal foreground color
123 * `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` is `0`:
124 * Output bright colors using dedicated SGR codes
128 * Output bright colors with bold + SGR codes 30 to
131 term_env_var
= getenv("TERM");
133 if (term_env_var
&& strcmp(term_env_var
, "xterm-kitty") == 0) {
135 * The kitty terminal emulator supports
136 * non-bright bold foreground colors.
138 bright_means_bold
= false;
141 bright_means_bold_env_var
=
142 getenv("BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD");
144 if (bright_means_bold_env_var
) {
146 !(strcmp(bright_means_bold_env_var
, "0") == 0);
149 if (bright_means_bold
) {
150 code_fg_bright_red
= BT_COMMON_COLOR_FG_BOLD_RED
;
151 code_fg_bright_green
= BT_COMMON_COLOR_FG_BOLD_GREEN
;
152 code_fg_bright_yellow
= BT_COMMON_COLOR_FG_BOLD_YELLOW
;
153 code_fg_bright_blue
= BT_COMMON_COLOR_FG_BOLD_BLUE
;
154 code_fg_bright_magenta
= BT_COMMON_COLOR_FG_BOLD_MAGENTA
;
155 code_fg_bright_cyan
= BT_COMMON_COLOR_FG_BOLD_CYAN
;
156 code_fg_bright_light_gray
= BT_COMMON_COLOR_FG_BOLD_LIGHT_GRAY
;
158 code_fg_bright_red
= BT_COMMON_COLOR_FG_BRIGHT_RED
;
159 code_fg_bright_green
= BT_COMMON_COLOR_FG_BRIGHT_GREEN
;
160 code_fg_bright_yellow
= BT_COMMON_COLOR_FG_BRIGHT_YELLOW
;
161 code_fg_bright_blue
= BT_COMMON_COLOR_FG_BRIGHT_BLUE
;
162 code_fg_bright_magenta
= BT_COMMON_COLOR_FG_BRIGHT_MAGENTA
;
163 code_fg_bright_cyan
= BT_COMMON_COLOR_FG_BRIGHT_CYAN
;
164 code_fg_bright_light_gray
= BT_COMMON_COLOR_FG_BRIGHT_LIGHT_GRAY
;
167 if (bt_common_colors_supported()) {
168 bt_common_color_code_reset
= BT_COMMON_COLOR_RESET
;
169 bt_common_color_code_bold
= BT_COMMON_COLOR_BOLD
;
170 bt_common_color_code_fg_default
= BT_COMMON_COLOR_FG_DEFAULT
;
171 bt_common_color_code_fg_red
= BT_COMMON_COLOR_FG_RED
;
172 bt_common_color_code_fg_green
= BT_COMMON_COLOR_FG_GREEN
;
173 bt_common_color_code_fg_yellow
= BT_COMMON_COLOR_FG_YELLOW
;
174 bt_common_color_code_fg_blue
= BT_COMMON_COLOR_FG_BLUE
;
175 bt_common_color_code_fg_magenta
= BT_COMMON_COLOR_FG_MAGENTA
;
176 bt_common_color_code_fg_cyan
= BT_COMMON_COLOR_FG_CYAN
;
177 bt_common_color_code_fg_light_gray
= BT_COMMON_COLOR_FG_LIGHT_GRAY
;
179 bt_common_color_code_fg_bright_red
= code_fg_bright_red
;
180 bt_common_color_code_fg_bright_green
= code_fg_bright_green
;
181 bt_common_color_code_fg_bright_yellow
= code_fg_bright_yellow
;
182 bt_common_color_code_fg_bright_blue
= code_fg_bright_blue
;
183 bt_common_color_code_fg_bright_magenta
= code_fg_bright_magenta
;
184 bt_common_color_code_fg_bright_cyan
= code_fg_bright_cyan
;
185 bt_common_color_code_fg_bright_light_gray
= code_fg_bright_light_gray
;
187 bt_common_color_code_bg_default
= BT_COMMON_COLOR_BG_DEFAULT
;
188 bt_common_color_code_bg_red
= BT_COMMON_COLOR_BG_RED
;
189 bt_common_color_code_bg_green
= BT_COMMON_COLOR_BG_GREEN
;
190 bt_common_color_code_bg_yellow
= BT_COMMON_COLOR_BG_YELLOW
;
191 bt_common_color_code_bg_blue
= BT_COMMON_COLOR_BG_BLUE
;
192 bt_common_color_code_bg_magenta
= BT_COMMON_COLOR_BG_MAGENTA
;
193 bt_common_color_code_bg_cyan
= BT_COMMON_COLOR_BG_CYAN
;
194 bt_common_color_code_bg_light_gray
= BT_COMMON_COLOR_BG_LIGHT_GRAY
;
197 color_codes
.reset
= BT_COMMON_COLOR_RESET
;
198 color_codes
.bold
= BT_COMMON_COLOR_BOLD
;
199 color_codes
.fg_default
= BT_COMMON_COLOR_FG_DEFAULT
;
200 color_codes
.fg_red
= BT_COMMON_COLOR_FG_RED
;
201 color_codes
.fg_green
= BT_COMMON_COLOR_FG_GREEN
;
202 color_codes
.fg_yellow
= BT_COMMON_COLOR_FG_YELLOW
;
203 color_codes
.fg_blue
= BT_COMMON_COLOR_FG_BLUE
;
204 color_codes
.fg_magenta
= BT_COMMON_COLOR_FG_MAGENTA
;
205 color_codes
.fg_cyan
= BT_COMMON_COLOR_FG_CYAN
;
206 color_codes
.fg_light_gray
= BT_COMMON_COLOR_FG_LIGHT_GRAY
;
207 color_codes
.fg_bright_red
= code_fg_bright_red
;
208 color_codes
.fg_bright_green
= code_fg_bright_green
;
209 color_codes
.fg_bright_yellow
= code_fg_bright_yellow
;
210 color_codes
.fg_bright_blue
= code_fg_bright_blue
;
211 color_codes
.fg_bright_magenta
= code_fg_bright_magenta
;
212 color_codes
.fg_bright_cyan
= code_fg_bright_cyan
;
213 color_codes
.fg_bright_light_gray
= code_fg_bright_light_gray
;
214 color_codes
.bg_default
= BT_COMMON_COLOR_BG_DEFAULT
;
215 color_codes
.bg_red
= BT_COMMON_COLOR_BG_RED
;
216 color_codes
.bg_green
= BT_COMMON_COLOR_BG_GREEN
;
217 color_codes
.bg_yellow
= BT_COMMON_COLOR_BG_YELLOW
;
218 color_codes
.bg_blue
= BT_COMMON_COLOR_BG_BLUE
;
219 color_codes
.bg_magenta
= BT_COMMON_COLOR_BG_MAGENTA
;
220 color_codes
.bg_cyan
= BT_COMMON_COLOR_BG_CYAN
;
221 color_codes
.bg_light_gray
= BT_COMMON_COLOR_BG_LIGHT_GRAY
;
225 const char *bt_common_get_system_plugin_path(void)
227 return SYSTEM_PLUGIN_PATH
;
232 bool bt_common_is_setuid_setgid(void)
236 #else /* __MINGW32__ */
238 bool bt_common_is_setuid_setgid(void)
240 return (geteuid() != getuid() || getegid() != getgid());
242 #endif /* __MINGW32__ */
246 const char *bt_get_home_dir(int log_level
)
248 return g_get_home_dir();
250 #else /* __MINGW32__ */
252 char *bt_secure_getenv(const char *name
, int log_level
)
254 if (bt_common_is_setuid_setgid()) {
255 BT_LOGD("Disregarding environment variable for setuid/setgid binary: "
256 "name=\"%s\"", name
);
263 const char *bt_get_home_dir(int log_level
)
268 val
= bt_secure_getenv(HOME_ENV_VAR
, log_level
);
272 /* Fallback on password file. */
273 pwd
= getpwuid(getuid());
281 #endif /* __MINGW32__ */
284 char *bt_common_get_home_plugin_path(int log_level
)
287 const char *home_dir
;
290 home_dir
= bt_get_home_dir(log_level
);
295 length
= strlen(home_dir
) + strlen(HOME_PLUGIN_SUBPATH
) + 1;
297 if (length
>= PATH_MAX
) {
298 BT_LOGW("Home directory path is too long: "
299 "length=%zu, max-length=%u", length
, PATH_MAX
);
303 path
= malloc(PATH_MAX
);
308 strcpy(path
, home_dir
);
309 strcat(path
, HOME_PLUGIN_SUBPATH
);
316 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
)
321 size_t init_dirs_len
;
324 init_dirs_len
= dirs
->len
;
327 /* Nothing to append */
332 end
= paths
+ strlen(paths
);
336 const char *next_sep
;
338 next_sep
= strchr(at
, G_SEARCHPATH_SEPARATOR
);
339 if (next_sep
== at
) {
341 * Empty path: try next character (supported
342 * to conform to the typical parsing of $PATH).
346 } else if (!next_sep
) {
347 /* No more separator: use the remaining */
348 next_sep
= paths
+ strlen(paths
);
351 path
= g_string_new(NULL
);
356 g_string_append_len(path
, at
, next_sep
- at
);
358 g_ptr_array_add(dirs
, path
);
366 /* Remove the new entries in dirs */
367 while (dirs
->len
> init_dirs_len
) {
368 g_ptr_array_remove_index(dirs
, init_dirs_len
);
376 bool isarealtty(int fd
)
379 struct stat tty_stats
;
386 if (fstat(fd
, &tty_stats
) == 0) {
387 if (!S_ISCHR(tty_stats
.st_mode
)) {
388 /* Not a character device: not a TTY */
400 bool bt_common_colors_supported(void)
402 static bool supports_colors
= false;
403 static bool supports_colors_set
= false;
404 const char *term_env_var
;
405 const char *term_color_env_var
;
407 if (supports_colors_set
) {
411 supports_colors_set
= true;
414 * `BABELTRACE_TERM_COLOR` environment variable always overrides
415 * the automatic color support detection.
417 term_color_env_var
= getenv("BABELTRACE_TERM_COLOR");
418 if (term_color_env_var
) {
419 if (g_ascii_strcasecmp(term_color_env_var
, "always") == 0) {
421 supports_colors
= true;
422 } else if (g_ascii_strcasecmp(term_color_env_var
, "never") == 0) {
423 /* Force no colors */
428 /* We need a compatible, known terminal */
429 term_env_var
= getenv("TERM");
434 if (strncmp(term_env_var
, "xterm", 5) != 0 &&
435 strncmp(term_env_var
, "rxvt", 4) != 0 &&
436 strncmp(term_env_var
, "konsole", 7) != 0 &&
437 strncmp(term_env_var
, "gnome", 5) != 0 &&
438 strncmp(term_env_var
, "screen", 5) != 0 &&
439 strncmp(term_env_var
, "tmux", 4) != 0 &&
440 strncmp(term_env_var
, "putty", 5) != 0) {
444 /* Both standard output and error streams need to be TTYs */
445 if (!isarealtty(STDOUT_FILENO
) || !isarealtty(STDERR_FILENO
)) {
449 supports_colors
= true;
452 return supports_colors
;
456 const char *bt_common_color_reset(void)
458 return bt_common_color_code_reset
;
462 const char *bt_common_color_bold(void)
464 return bt_common_color_code_bold
;
468 const char *bt_common_color_fg_default(void)
470 return bt_common_color_code_fg_default
;
474 const char *bt_common_color_fg_red(void)
476 return bt_common_color_code_fg_red
;
480 const char *bt_common_color_fg_green(void)
482 return bt_common_color_code_fg_green
;
486 const char *bt_common_color_fg_yellow(void)
488 return bt_common_color_code_fg_yellow
;
492 const char *bt_common_color_fg_blue(void)
494 return bt_common_color_code_fg_blue
;
498 const char *bt_common_color_fg_magenta(void)
500 return bt_common_color_code_fg_magenta
;
504 const char *bt_common_color_fg_cyan(void)
506 return bt_common_color_code_fg_cyan
;
510 const char *bt_common_color_fg_light_gray(void)
512 return bt_common_color_code_fg_light_gray
;
516 const char *bt_common_color_fg_bright_red(void)
518 return bt_common_color_code_fg_bright_red
;
522 const char *bt_common_color_fg_bright_green(void)
524 return bt_common_color_code_fg_bright_green
;
528 const char *bt_common_color_fg_bright_yellow(void)
530 return bt_common_color_code_fg_bright_yellow
;
534 const char *bt_common_color_fg_bright_blue(void)
536 return bt_common_color_code_fg_bright_blue
;
540 const char *bt_common_color_fg_bright_magenta(void)
542 return bt_common_color_code_fg_bright_magenta
;
546 const char *bt_common_color_fg_bright_cyan(void)
548 return bt_common_color_code_fg_bright_cyan
;
552 const char *bt_common_color_fg_bright_light_gray(void)
554 return bt_common_color_code_fg_bright_light_gray
;
558 const char *bt_common_color_bg_default(void)
560 return bt_common_color_code_bg_default
;
564 const char *bt_common_color_bg_red(void)
566 return bt_common_color_code_bg_red
;
570 const char *bt_common_color_bg_green(void)
572 return bt_common_color_code_bg_green
;
576 const char *bt_common_color_bg_yellow(void)
578 return bt_common_color_code_bg_yellow
;
582 const char *bt_common_color_bg_blue(void)
584 return bt_common_color_code_bg_blue
;
588 const char *bt_common_color_bg_magenta(void)
590 return bt_common_color_code_bg_magenta
;
594 const char *bt_common_color_bg_cyan(void)
596 return bt_common_color_code_bg_cyan
;
600 const char *bt_common_color_bg_light_gray(void)
602 return bt_common_color_code_bg_light_gray
;
606 void bt_common_color_get_codes(struct bt_common_color_codes
*codes
,
607 enum bt_common_color_when use_colors
)
609 if (use_colors
== BT_COMMON_COLOR_WHEN_ALWAYS
) {
610 *codes
= color_codes
;
611 } else if (use_colors
== BT_COMMON_COLOR_WHEN_NEVER
) {
612 *codes
= no_color_codes
;
614 BT_ASSERT(use_colors
== BT_COMMON_COLOR_WHEN_AUTO
);
616 if (bt_common_colors_supported()) {
617 *codes
= color_codes
;
619 *codes
= no_color_codes
;
625 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
626 const char *end_chars
, size_t *end_pos
)
628 GString
*output
= g_string_new(NULL
);
631 const char *end_char
;
637 for (ch
= input
; *ch
!= '\0'; ch
++) {
639 bool continue_loop
= false;
642 /* `\` at the end of the string: append `\` */
643 g_string_append_c(output
, *ch
);
648 for (es_char
= escapable_chars
; *es_char
!= '\0'; es_char
++) {
649 if (ch
[1] == *es_char
) {
651 * `\` followed by an escapable
652 * character: append the escaped
655 g_string_append_c(output
, ch
[1]);
657 continue_loop
= true;
667 * `\` followed by a non-escapable character:
668 * append `\` and the character.
670 g_string_append_c(output
, *ch
);
671 g_string_append_c(output
, ch
[1]);
675 for (end_char
= end_chars
; *end_char
!= '\0'; end_char
++) {
676 if (*ch
== *end_char
) {
678 * End character found:
679 * terminate this loop.
685 /* Normal character: append */
686 g_string_append_c(output
, *ch
);
692 *end_pos
= ch
- input
;
699 g_string_free(output
, TRUE
);
708 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
)
710 GString
*output
= g_string_new(NULL
);
712 bool no_quote
= true;
718 if (strlen(input
) == 0) {
719 if (with_single_quotes
) {
720 g_string_assign(output
, "''");
726 for (ch
= input
; *ch
!= '\0'; ch
++) {
729 if (!g_ascii_isalpha(c
) && !g_ascii_isdigit(c
) && c
!= '_' &&
730 c
!= '@' && c
!= '%' && c
!= '+' && c
!= '=' &&
731 c
!= ':' && c
!= ',' && c
!= '.' && c
!= '/' &&
739 g_string_assign(output
, input
);
743 if (with_single_quotes
) {
744 g_string_assign(output
, "'");
747 for (ch
= input
; *ch
!= '\0'; ch
++) {
749 g_string_append(output
, "'\"'\"'");
751 g_string_append_c(output
, *ch
);
755 if (with_single_quotes
) {
756 g_string_append_c(output
, '\'');
764 bool bt_common_string_is_printable(const char *input
)
767 bool printable
= true;
768 BT_ASSERT_DBG(input
);
770 for (ch
= input
; *ch
!= '\0'; ch
++) {
771 if (!isprint((unsigned char) *ch
) && *ch
!= '\n' && *ch
!= '\r' &&
772 *ch
!= '\t' && *ch
!= '\v') {
783 void bt_common_destroy_lttng_live_url_parts(
784 struct bt_common_lttng_live_url_parts
*parts
)
791 g_string_free(parts
->proto
, TRUE
);
795 if (parts
->hostname
) {
796 g_string_free(parts
->hostname
, TRUE
);
797 parts
->hostname
= NULL
;
800 if (parts
->target_hostname
) {
801 g_string_free(parts
->target_hostname
, TRUE
);
802 parts
->target_hostname
= NULL
;
805 if (parts
->session_name
) {
806 g_string_free(parts
->session_name
, TRUE
);
807 parts
->session_name
= NULL
;
815 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
816 const char *url
, char *error_buf
, size_t error_buf_size
)
818 struct bt_common_lttng_live_url_parts parts
;
819 const char *at
= url
;
823 memset(&parts
, 0, sizeof(parts
));
827 parts
.proto
= bt_common_string_until(at
, "", ":", &end_pos
);
828 if (!parts
.proto
|| parts
.proto
->len
== 0) {
830 snprintf(error_buf
, error_buf_size
, "Missing protocol");
836 if (strcmp(parts
.proto
->str
, "net") == 0) {
837 g_string_assign(parts
.proto
, "net4");
840 if (strcmp(parts
.proto
->str
, "net4") != 0 &&
841 strcmp(parts
.proto
->str
, "net6") != 0) {
843 snprintf(error_buf
, error_buf_size
,
844 "Unknown protocol: `%s`", parts
.proto
->str
);
850 if (at
[end_pos
] != ':') {
852 snprintf(error_buf
, error_buf_size
,
853 "Expecting `:` after `%s`", parts
.proto
->str
);
862 if (strncmp(at
, "://", 3) != 0) {
864 snprintf(error_buf
, error_buf_size
,
865 "Expecting `://` after protocol");
875 parts
.hostname
= bt_common_string_until(at
, "", ":/", &end_pos
);
876 if (!parts
.hostname
|| parts
.hostname
->len
== 0) {
878 snprintf(error_buf
, error_buf_size
, "Missing hostname");
884 if (at
[end_pos
] == ':') {
889 port
= bt_common_string_until(at
, "", "/", &end_pos
);
890 if (!port
|| port
->len
== 0) {
892 snprintf(error_buf
, error_buf_size
, "Missing port");
898 if (sscanf(port
->str
, "%d", &parts
.port
) != 1) {
900 snprintf(error_buf
, error_buf_size
,
901 "Invalid port: `%s`", port
->str
);
904 g_string_free(port
, TRUE
);
908 g_string_free(port
, TRUE
);
910 if (parts
.port
< 0 || parts
.port
>= 65536) {
912 snprintf(error_buf
, error_buf_size
,
913 "Invalid port: %d", parts
.port
);
920 if (at
[end_pos
] == '\0') {
921 /* Relay daemon hostname and ports provided only */
928 if (strncmp(at
, "/host/", 6) != 0) {
930 snprintf(error_buf
, error_buf_size
,
931 "Expecting `/host/` after hostname or port");
939 /* Target hostname */
940 parts
.target_hostname
= bt_common_string_until(at
, "", "/", &end_pos
);
941 if (!parts
.target_hostname
|| parts
.target_hostname
->len
== 0) {
943 snprintf(error_buf
, error_buf_size
,
944 "Missing target hostname");
950 if (at
[end_pos
] == '\0') {
952 snprintf(error_buf
, error_buf_size
,
953 "Missing `/` after target hostname (`%s`)",
954 parts
.target_hostname
->str
);
964 parts
.session_name
= bt_common_string_until(at
, "", "/", &end_pos
);
965 if (!parts
.session_name
|| parts
.session_name
->len
== 0) {
967 snprintf(error_buf
, error_buf_size
,
968 "Missing session name");
974 if (at
[end_pos
] == '/') {
976 snprintf(error_buf
, error_buf_size
,
977 "Unexpected `/` after session name (`%s`)",
978 parts
.session_name
->str
);
987 bt_common_destroy_lttng_live_url_parts(&parts
);
994 void bt_common_normalize_star_glob_pattern(char *pattern
)
998 bool got_star
= false;
1002 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
1006 /* Avoid consecutive stars. */
1013 /* Copy backslash character. */
1028 /* Copy single character. */
1038 bool at_end_of_pattern(const char *p
, const char *pattern
, size_t pattern_len
)
1040 return (p
- pattern
) == pattern_len
|| *p
== '\0';
1044 * Globbing matching function with the star feature only (`?` and
1045 * character sets are not supported). This matches `candidate` (plain
1046 * string) against `pattern`. A literal star can be escaped with `\` in
1049 * `pattern_len` or `candidate_len` can be greater than the actual
1050 * string length of `pattern` or `candidate` if the string is
1054 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
1055 const char *candidate
, size_t candidate_len
) {
1056 const char *retry_c
= candidate
, *retry_p
= pattern
, *c
, *p
;
1057 bool got_a_star
= false;
1064 * The concept here is to retry a match in the specific case
1065 * where we already got a star. The retry position for the
1066 * pattern is just after the most recent star, and the retry
1067 * position for the candidate is the character following the
1068 * last try's first character.
1072 * candidate: hi ev every onyx one
1074 * pattern: hi*every*one
1077 * candidate: hi ev every onyx one
1079 * pattern: hi*every*one
1082 * candidate: hi ev every onyx one
1084 * pattern: hi*every*one
1087 * candidate: hi ev every onyx one
1089 * pattern: hi*every*one
1092 * candidate: hi ev every onyx one
1094 * pattern: hi*every*one
1097 * candidate: hi ev every onyx one
1099 * pattern: hi*every*one
1102 * candidate: hi ev every onyx one
1104 * pattern: hi*every*one
1107 * candidate: hi ev every onyx one
1109 * pattern: hi*every*one
1112 * candidate: hi ev every onyx one
1114 * pattern: hi*every*one
1117 * candidate: hi ev every onyx one
1119 * pattern: hi*every*one
1122 * candidate: hi ev every onyx one
1124 * pattern: hi*every*one
1127 * candidate: hi ev every onyx one
1129 * pattern: hi*every*one
1132 * candidate: hi ev every onyx one
1134 * pattern: hi*every*one
1137 * candidate: hi ev every onyx one
1139 * pattern: hi*every*one
1142 * candidate: hi ev every onyx one
1144 * pattern: hi*every*one
1147 * candidate: hi ev every onyx one
1149 * pattern: hi*every*one
1152 * candidate: hi ev every onyx one
1154 * pattern: hi*every*one
1157 * candidate: hi ev every onyx one
1159 * pattern: hi*every*one
1162 * candidate: hi ev every onyx one
1164 * pattern: hi*every*one
1167 * candidate: hi ev every onyx one
1169 * pattern: hi*every*one
1172 * candidate: hi ev every onyx one
1174 * pattern: hi*every*one
1177 * candidate: hi ev every onyx one
1179 * pattern: hi*every*one
1182 * candidate: hi ev every onyx one
1184 * pattern: hi*every*one
1187 * candidate: hi ev every onyx one
1189 * pattern: hi*every*one
1192 * candidate: hi ev every onyx one
1194 * pattern: hi*every*one
1197 * candidate: hi ev every onyx one
1199 * pattern: hi*every*one
1202 * candidate: hi ev every onyx one
1204 * pattern: hi*every*one
1207 while ((c
- candidate
) < candidate_len
&& *c
!= '\0') {
1210 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1211 goto end_of_pattern
;
1219 * Our first try starts at the current candidate
1220 * character and after the star in the pattern.
1225 if (at_end_of_pattern(retry_p
, pattern
, pattern_len
)) {
1227 * Star at the end of the pattern at
1228 * this point: automatic match.
1235 /* Go to escaped character. */
1239 * Fall through the default case which compares
1240 * the escaped character now.
1244 if (at_end_of_pattern(p
, pattern
, pattern_len
) ||
1247 /* Character mismatch OR end of pattern. */
1250 * We didn't get any star yet,
1251 * so this first mismatch
1252 * automatically makes the whole
1259 * Next try: next candidate character,
1260 * original pattern character (following
1261 * the most recent star).
1269 /* Next pattern and candidate characters. */
1275 * We checked every candidate character and we're still in a
1276 * success state: the only pattern character allowed to remain
1279 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1284 return p
[-1] == '*' && at_end_of_pattern(p
, pattern
, pattern_len
);
1289 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1292 GString
*norm_path
= NULL
;
1296 tmp
= _fullpath(NULL
, path
, PATH_MAX
);
1301 norm_path
= g_string_new(tmp
);
1309 g_string_free(norm_path
, TRUE
);
1318 void append_path_parts(const char *path
, GPtrArray
*parts
)
1320 const char *ch
= path
;
1321 const char *last
= path
;
1324 if (*ch
== G_DIR_SEPARATOR
|| *ch
== '\0') {
1325 if (ch
- last
> 0) {
1326 GString
*part
= g_string_new(NULL
);
1329 g_string_append_len(part
, last
, ch
- last
);
1330 g_ptr_array_add(parts
, part
);
1345 void destroy_gstring(void *gstring
)
1347 (void) g_string_free(gstring
, TRUE
);
1351 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1355 GPtrArray
*parts
= NULL
;
1358 norm_path
= g_string_new(G_DIR_SEPARATOR_S
);
1363 parts
= g_ptr_array_new_with_free_func(destroy_gstring
);
1368 if (path
[0] != G_DIR_SEPARATOR
) {
1369 /* Relative path: start with working directory */
1371 append_path_parts(wd
, parts
);
1373 gchar
*cd
= g_get_current_dir();
1375 append_path_parts(cd
, parts
);
1380 /* Append parts of the path parameter */
1381 append_path_parts(path
, parts
);
1383 /* Resolve special `..` and `.` parts */
1384 for (i
= 0; i
< parts
->len
; i
++) {
1385 GString
*part
= g_ptr_array_index(parts
, i
);
1387 if (strcmp(part
->str
, "..") == 0) {
1390 * First part of absolute path is `..`:
1396 /* Remove `..` and previous part */
1397 g_ptr_array_remove_index(parts
, i
- 1);
1398 g_ptr_array_remove_index(parts
, i
- 1);
1400 } else if (strcmp(part
->str
, ".") == 0) {
1402 g_ptr_array_remove_index(parts
, i
);
1407 /* Create normalized path with what's left */
1408 for (i
= 0; i
< parts
->len
; i
++) {
1409 GString
*part
= g_ptr_array_index(parts
, i
);
1411 g_string_append(norm_path
, part
->str
);
1413 if (i
< parts
->len
- 1) {
1414 g_string_append_c(norm_path
, G_DIR_SEPARATOR
);
1422 g_string_free(norm_path
, TRUE
);
1428 g_ptr_array_free(parts
, TRUE
);
1436 size_t bt_common_get_page_size(int log_level
)
1440 page_size
= bt_sysconf(_SC_PAGESIZE
);
1441 if (page_size
< 0) {
1442 BT_LOGF("Cannot get system's page size: ret=%d",
1450 #define BUF_STD_APPEND(...) \
1452 char _tmp_fmt[64]; \
1454 size_t _size = buf_size - (size_t) (*buf_ch - buf); \
1455 size_t _tmp_fmt_size = (size_t) (fmt_ch - *out_fmt_ch); \
1456 strncpy(_tmp_fmt, *out_fmt_ch, _tmp_fmt_size); \
1457 _tmp_fmt[_tmp_fmt_size] = '\0'; \
1458 _Pragma("GCC diagnostic push") \
1459 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"") \
1460 _count = snprintf(*buf_ch, _size, _tmp_fmt, __VA_ARGS__); \
1461 _Pragma("GCC diagnostic pop") \
1462 BT_ASSERT_DBG(_count >= 0); \
1463 *buf_ch += MIN(_count, _size); \
1466 #define BUF_STD_APPEND_SINGLE_ARG(_type) \
1468 _type _arg = va_arg(*args, _type); \
1469 BUF_STD_APPEND(_arg); \
1472 static inline void handle_conversion_specifier_std(char *buf
, char **buf_ch
,
1473 size_t buf_size
, const char **out_fmt_ch
, va_list *args
)
1475 const char *fmt_ch
= *out_fmt_ch
;
1476 enum LENGTH_MODIFIER
{
1484 } length_mod
= LENGTH_MOD_NONE
;
1489 if (*fmt_ch
== '%') {
1515 if (*fmt_ch
< '0' || *fmt_ch
> '9') {
1523 if (*fmt_ch
== '.') {
1527 if (*fmt_ch
< '0' || *fmt_ch
> '9') {
1535 /* format (PRI*64) */
1536 if (strncmp(fmt_ch
, PRId64
, sizeof(PRId64
) - 1) == 0) {
1537 fmt_ch
+= sizeof(PRId64
) - 1;
1538 BUF_STD_APPEND_SINGLE_ARG(int64_t);
1540 } else if (strncmp(fmt_ch
, PRIu64
, sizeof(PRIu64
) - 1) == 0) {
1541 fmt_ch
+= sizeof(PRIu64
) - 1;
1542 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1544 } else if (strncmp(fmt_ch
, PRIx64
, sizeof(PRIx64
) - 1) == 0) {
1545 fmt_ch
+= sizeof(PRIx64
) - 1;
1546 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1548 } else if (strncmp(fmt_ch
, PRIX64
, sizeof(PRIX64
) - 1) == 0) {
1549 fmt_ch
+= sizeof(PRIX64
) - 1;
1550 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1552 } else if (strncmp(fmt_ch
, PRIo64
, sizeof(PRIo64
) - 1) == 0) {
1553 fmt_ch
+= sizeof(PRIo64
) - 1;
1554 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1556 } else if (strncmp(fmt_ch
, PRIi64
, sizeof(PRIi64
) - 1) == 0) {
1557 fmt_ch
+= sizeof(PRIi64
) - 1;
1558 BUF_STD_APPEND_SINGLE_ARG(int64_t);
1565 length_mod
= LENGTH_MOD_H
;
1568 if (*fmt_ch
== 'h') {
1569 length_mod
= LENGTH_MOD_HH
;
1575 length_mod
= LENGTH_MOD_LOW_L
;
1578 if (*fmt_ch
== 'l') {
1579 length_mod
= LENGTH_MOD_LOW_LL
;
1585 length_mod
= LENGTH_MOD_UP_L
;
1589 length_mod
= LENGTH_MOD_Z
;
1602 switch (length_mod
) {
1603 case LENGTH_MOD_NONE
:
1604 case LENGTH_MOD_LOW_L
:
1605 BUF_STD_APPEND_SINGLE_ARG(int);
1615 switch (length_mod
) {
1616 case LENGTH_MOD_NONE
:
1617 BUF_STD_APPEND_SINGLE_ARG(char *);
1619 case LENGTH_MOD_LOW_L
:
1620 BUF_STD_APPEND_SINGLE_ARG(wchar_t *);
1630 switch (length_mod
) {
1631 case LENGTH_MOD_NONE
:
1634 BUF_STD_APPEND_SINGLE_ARG(int);
1636 case LENGTH_MOD_LOW_L
:
1637 BUF_STD_APPEND_SINGLE_ARG(long);
1639 case LENGTH_MOD_LOW_LL
:
1640 BUF_STD_APPEND_SINGLE_ARG(long long);
1643 BUF_STD_APPEND_SINGLE_ARG(size_t);
1655 switch (length_mod
) {
1656 case LENGTH_MOD_NONE
:
1659 BUF_STD_APPEND_SINGLE_ARG(unsigned int);
1661 case LENGTH_MOD_LOW_L
:
1662 BUF_STD_APPEND_SINGLE_ARG(unsigned long);
1664 case LENGTH_MOD_LOW_LL
:
1665 BUF_STD_APPEND_SINGLE_ARG(unsigned long long);
1668 BUF_STD_APPEND_SINGLE_ARG(size_t);
1682 switch (length_mod
) {
1683 case LENGTH_MOD_NONE
:
1684 BUF_STD_APPEND_SINGLE_ARG(double);
1686 case LENGTH_MOD_UP_L
:
1687 BUF_STD_APPEND_SINGLE_ARG(long double);
1696 if (length_mod
== LENGTH_MOD_NONE
) {
1697 BUF_STD_APPEND_SINGLE_ARG(void *);
1707 *out_fmt_ch
= fmt_ch
;
1711 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
1713 bt_common_handle_custom_specifier_func handle_specifier
,
1714 void *priv_data
, const char *fmt
, va_list *args
)
1716 const char *fmt_ch
= fmt
;
1722 while (*fmt_ch
!= '\0') {
1725 BT_ASSERT_DBG(fmt_ch
[1] != '\0');
1727 if (fmt_ch
[1] == intro
) {
1728 handle_specifier(priv_data
, &buf_ch
,
1729 buf_size
- (size_t) (buf_ch
- buf
),
1732 handle_conversion_specifier_std(buf
, &buf_ch
,
1733 buf_size
, &fmt_ch
, args
);
1736 if (buf_ch
>= buf
+ buf_size
- 1) {
1743 if (buf_ch
>= buf
+ buf_size
- 1) {
1755 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
1757 bt_common_handle_custom_specifier_func handle_specifier
,
1758 void *priv_data
, const char *fmt
, ...)
1761 va_start(args
, fmt
);
1762 bt_common_custom_vsnprintf(buf
, buf_size
, intro
, handle_specifier
,
1763 priv_data
, fmt
, &args
);
1768 void bt_common_sep_digits(char *str
, unsigned int digits_per_group
, char sep
)
1777 BT_ASSERT_DBG(digits_per_group
> 0);
1778 BT_ASSERT_DBG(sep
!= '\0');
1780 /* Compute new length of `str` */
1781 orig_len
= strlen(str
);
1782 BT_ASSERT_DBG(orig_len
> 0);
1783 sep_count
= (orig_len
- 1) / digits_per_group
;
1784 new_len
= strlen(str
) + sep_count
;
1787 * Do the work in place. Have the reading pointer `rd` start at
1788 * the end of the original string, and the writing pointer `wr`
1789 * start at the end of the new string, making sure to also put a
1790 * null character there.
1792 rd
= str
+ orig_len
- 1;
1798 * Here's what the process looks like (3 digits per group):
1802 * Destination: 12345678#8
1807 * Destination: 1234567878
1812 * Destination: 1234567678
1817 * Destination: 123456,678
1822 * Destination: 123455,678
1827 * Destination: 123445,678
1832 * Destination: 123345,678
1837 * Destination: 12,345,678
1842 * Destination: 12,345,678
1847 * Destination: 12,345,678
1850 while (rd
!= str
- 1) {
1851 if (i
== digits_per_group
) {
1853 * Time to append the separator: decrement `wr`,
1854 * but keep `rd` as is.
1862 /* Copy read-side character to write-side character */
1871 GString
*bt_common_fold(const char *str
, unsigned int total_length
,
1872 unsigned int indent
)
1874 const unsigned int content_length
= total_length
- indent
;
1875 GString
*folded
= g_string_new(NULL
);
1876 GString
*tmp_line
= g_string_new(NULL
);
1877 gchar
**lines
= NULL
;
1878 gchar
**line_words
= NULL
;
1879 gchar
* const *line
;
1883 BT_ASSERT_DBG(indent
< total_length
);
1884 BT_ASSERT_DBG(tmp_line
);
1885 BT_ASSERT_DBG(folded
);
1887 if (strlen(str
) == 0) {
1888 /* Empty input string: empty output string */
1893 lines
= g_strsplit(str
, "\n", 0);
1894 BT_ASSERT_DBG(lines
);
1896 /* For each source line */
1897 for (line
= lines
; *line
; line
++) {
1898 gchar
* const *word
;
1901 * Append empty line without indenting if source line is
1904 if (strlen(*line
) == 0) {
1905 g_string_append_c(folded
, '\n');
1910 line_words
= g_strsplit(*line
, " ", 0);
1911 BT_ASSERT_DBG(line_words
);
1914 * Indent for first line (we know there's at least one
1915 * word at this point).
1917 for (i
= 0; i
< indent
; i
++) {
1918 g_string_append_c(folded
, ' ');
1921 /* Append words, folding when necessary */
1922 g_string_assign(tmp_line
, "");
1924 for (word
= line_words
; *word
; word
++) {
1926 * `tmp_line->len > 0` is in the condition so
1927 * that words that are larger than
1928 * `content_length` are at least written on
1931 * `tmp_line->len - 1` because the temporary
1932 * line always contains a trailing space which
1933 * won't be part of the line if we fold.
1935 if (tmp_line
->len
> 0 &&
1936 tmp_line
->len
- 1 + strlen(*word
) >= content_length
) {
1937 /* Fold (without trailing space) */
1938 g_string_append_len(folded
,
1939 tmp_line
->str
, tmp_line
->len
- 1);
1940 g_string_append_c(folded
, '\n');
1942 /* Indent new line */
1943 for (i
= 0; i
< indent
; i
++) {
1944 g_string_append_c(folded
, ' ');
1947 g_string_assign(tmp_line
, "");
1950 /* Append current word and space to temporary line */
1951 g_string_append(tmp_line
, *word
);
1952 g_string_append_c(tmp_line
, ' ');
1955 /* Append last line if any, without trailing space */
1956 if (tmp_line
->len
> 0) {
1957 g_string_append_len(folded
, tmp_line
->str
,
1961 /* Append source newline */
1962 g_string_append_c(folded
, '\n');
1964 /* Free array of this line's words */
1965 g_strfreev(line_words
);
1969 /* Remove trailing newline if any */
1970 if (folded
->str
[folded
->len
- 1] == '\n') {
1971 g_string_truncate(folded
, folded
->len
- 1);
1979 BT_ASSERT_DBG(!line_words
);
1982 g_string_free(tmp_line
, TRUE
);
1990 int bt_common_get_term_size(unsigned int *width
, unsigned int *height
)
1992 /* Not supported on Windows yet */
1995 #else /* __MINGW32__ */
1997 int bt_common_get_term_size(unsigned int *width
, unsigned int *height
)
2000 struct winsize winsize
;
2002 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &winsize
) < 0) {
2008 *width
= (unsigned int) winsize
.ws_col
;
2012 *height
= (unsigned int) winsize
.ws_row
;
2018 #endif /* __MINGW32__ */
2021 int bt_common_g_string_append_printf(GString
*str
, const char *fmt
, ...)
2024 gsize len
, allocated_len
, available_len
;
2027 /* str->len excludes \0. */
2029 /* Explicitly exclude \0. */
2030 allocated_len
= str
->allocated_len
- 1;
2031 available_len
= allocated_len
- len
;
2033 str
->len
= allocated_len
;
2035 print_len
= vsnprintf(str
->str
+ len
, available_len
+ 1, fmt
, ap
);
2037 if (print_len
< 0) {
2040 if (G_UNLIKELY(available_len
< print_len
)) {
2042 g_string_set_size(str
, len
+ print_len
);
2044 print_len
= vsnprintf(str
->str
+ len
, print_len
+ 1, fmt
, ap
);
2047 str
->len
= len
+ print_len
;
2053 int bt_common_append_file_content_to_g_string(GString
*str
, FILE *fp
)
2055 const size_t chunk_size
= 4096;
2059 gsize orig_len
= str
->len
;
2063 buf
= g_malloc(chunk_size
);
2079 read_len
= fread(buf
, 1, chunk_size
, fp
);
2080 g_string_append_len(str
, buf
, read_len
);
2085 /* Remove what was appended */
2086 g_string_truncate(str
, orig_len
);
2094 void bt_common_abort(void)
2096 static const char * const exec_on_abort_env_name
=
2097 "BABELTRACE_EXEC_ON_ABORT";
2098 const char *env_exec_on_abort
;
2100 env_exec_on_abort
= getenv(exec_on_abort_env_name
);
2101 if (env_exec_on_abort
) {
2102 if (bt_common_is_setuid_setgid()) {
2106 (void) g_spawn_command_line_sync(env_exec_on_abort
,
2107 NULL
, NULL
, NULL
, NULL
);