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
;
224 const char *bt_common_get_system_plugin_path(void)
226 return SYSTEM_PLUGIN_PATH
;
230 bool bt_common_is_setuid_setgid(void)
234 #else /* __MINGW32__ */
235 bool bt_common_is_setuid_setgid(void)
237 return (geteuid() != getuid() || getegid() != getgid());
239 #endif /* __MINGW32__ */
243 const char *bt_get_home_dir(int log_level
__attribute__((unused
)))
245 return g_get_home_dir();
247 #else /* __MINGW32__ */
249 char *bt_secure_getenv(const char *name
, int log_level
__attribute__((unused
)))
251 if (bt_common_is_setuid_setgid()) {
252 BT_LOGD("Disregarding environment variable for setuid/setgid binary: "
253 "name=\"%s\"", name
);
260 const char *bt_get_home_dir(int log_level
)
265 val
= bt_secure_getenv(HOME_ENV_VAR
, log_level
);
269 /* Fallback on password file. */
270 pwd
= getpwuid(getuid());
278 #endif /* __MINGW32__ */
280 char *bt_common_get_home_plugin_path(int log_level
)
283 const char *home_dir
;
286 home_dir
= bt_get_home_dir(log_level
);
291 length
= strlen(home_dir
) + strlen(HOME_PLUGIN_SUBPATH
) + 1;
293 if (length
>= PATH_MAX
) {
294 BT_LOGW("Home directory path is too long: "
295 "length=%zu, max-length=%u", length
, PATH_MAX
);
299 path
= malloc(PATH_MAX
);
304 strcpy(path
, home_dir
);
305 strcat(path
, HOME_PLUGIN_SUBPATH
);
311 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
)
316 size_t init_dirs_len
;
319 init_dirs_len
= dirs
->len
;
322 /* Nothing to append */
327 end
= paths
+ strlen(paths
);
331 const char *next_sep
;
333 next_sep
= strchr(at
, G_SEARCHPATH_SEPARATOR
);
334 if (next_sep
== at
) {
336 * Empty path: try next character (supported
337 * to conform to the typical parsing of $PATH).
341 } else if (!next_sep
) {
342 /* No more separator: use the remaining */
343 next_sep
= paths
+ strlen(paths
);
346 path
= g_string_new(NULL
);
351 g_string_append_len(path
, at
, next_sep
- at
);
353 g_ptr_array_add(dirs
, path
);
361 /* Remove the new entries in dirs */
362 while (dirs
->len
> init_dirs_len
) {
363 g_ptr_array_remove_index(dirs
, init_dirs_len
);
371 bool isarealtty(int fd
)
374 struct stat tty_stats
;
381 if (fstat(fd
, &tty_stats
) == 0) {
382 if (!S_ISCHR(tty_stats
.st_mode
)) {
383 /* Not a character device: not a TTY */
394 bool bt_common_colors_supported(void)
396 static bool supports_colors
= false;
397 static bool supports_colors_set
= false;
398 const char *term_env_var
;
399 const char *term_color_env_var
;
401 if (supports_colors_set
) {
405 supports_colors_set
= true;
408 * `BABELTRACE_TERM_COLOR` environment variable always overrides
409 * the automatic color support detection.
411 term_color_env_var
= getenv("BABELTRACE_TERM_COLOR");
412 if (term_color_env_var
) {
413 if (g_ascii_strcasecmp(term_color_env_var
, "always") == 0) {
415 supports_colors
= true;
416 } else if (g_ascii_strcasecmp(term_color_env_var
, "never") == 0) {
417 /* Force no colors */
422 /* We need a compatible, known terminal */
423 term_env_var
= getenv("TERM");
428 if (strncmp(term_env_var
, "xterm", 5) != 0 &&
429 strncmp(term_env_var
, "rxvt", 4) != 0 &&
430 strncmp(term_env_var
, "konsole", 7) != 0 &&
431 strncmp(term_env_var
, "gnome", 5) != 0 &&
432 strncmp(term_env_var
, "screen", 5) != 0 &&
433 strncmp(term_env_var
, "tmux", 4) != 0 &&
434 strncmp(term_env_var
, "putty", 5) != 0) {
438 /* Both standard output and error streams need to be TTYs */
439 if (!isarealtty(STDOUT_FILENO
) || !isarealtty(STDERR_FILENO
)) {
443 supports_colors
= true;
446 return supports_colors
;
449 const char *bt_common_color_reset(void)
451 return bt_common_color_code_reset
;
454 const char *bt_common_color_bold(void)
456 return bt_common_color_code_bold
;
459 const char *bt_common_color_fg_default(void)
461 return bt_common_color_code_fg_default
;
464 const char *bt_common_color_fg_red(void)
466 return bt_common_color_code_fg_red
;
469 const char *bt_common_color_fg_green(void)
471 return bt_common_color_code_fg_green
;
474 const char *bt_common_color_fg_yellow(void)
476 return bt_common_color_code_fg_yellow
;
479 const char *bt_common_color_fg_blue(void)
481 return bt_common_color_code_fg_blue
;
484 const char *bt_common_color_fg_magenta(void)
486 return bt_common_color_code_fg_magenta
;
489 const char *bt_common_color_fg_cyan(void)
491 return bt_common_color_code_fg_cyan
;
494 const char *bt_common_color_fg_light_gray(void)
496 return bt_common_color_code_fg_light_gray
;
499 const char *bt_common_color_fg_bright_red(void)
501 return bt_common_color_code_fg_bright_red
;
504 const char *bt_common_color_fg_bright_green(void)
506 return bt_common_color_code_fg_bright_green
;
509 const char *bt_common_color_fg_bright_yellow(void)
511 return bt_common_color_code_fg_bright_yellow
;
514 const char *bt_common_color_fg_bright_blue(void)
516 return bt_common_color_code_fg_bright_blue
;
519 const char *bt_common_color_fg_bright_magenta(void)
521 return bt_common_color_code_fg_bright_magenta
;
524 const char *bt_common_color_fg_bright_cyan(void)
526 return bt_common_color_code_fg_bright_cyan
;
529 const char *bt_common_color_fg_bright_light_gray(void)
531 return bt_common_color_code_fg_bright_light_gray
;
534 const char *bt_common_color_bg_default(void)
536 return bt_common_color_code_bg_default
;
539 const char *bt_common_color_bg_red(void)
541 return bt_common_color_code_bg_red
;
544 const char *bt_common_color_bg_green(void)
546 return bt_common_color_code_bg_green
;
549 const char *bt_common_color_bg_yellow(void)
551 return bt_common_color_code_bg_yellow
;
554 const char *bt_common_color_bg_blue(void)
556 return bt_common_color_code_bg_blue
;
559 const char *bt_common_color_bg_magenta(void)
561 return bt_common_color_code_bg_magenta
;
564 const char *bt_common_color_bg_cyan(void)
566 return bt_common_color_code_bg_cyan
;
569 const char *bt_common_color_bg_light_gray(void)
571 return bt_common_color_code_bg_light_gray
;
574 void bt_common_color_get_codes(struct bt_common_color_codes
*codes
,
575 enum bt_common_color_when use_colors
)
577 if (use_colors
== BT_COMMON_COLOR_WHEN_ALWAYS
) {
578 *codes
= color_codes
;
579 } else if (use_colors
== BT_COMMON_COLOR_WHEN_NEVER
) {
580 *codes
= no_color_codes
;
582 BT_ASSERT(use_colors
== BT_COMMON_COLOR_WHEN_AUTO
);
584 if (bt_common_colors_supported()) {
585 *codes
= color_codes
;
587 *codes
= no_color_codes
;
592 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
593 const char *end_chars
, size_t *end_pos
)
595 GString
*output
= g_string_new(NULL
);
598 const char *end_char
;
604 for (ch
= input
; *ch
!= '\0'; ch
++) {
606 bool continue_loop
= false;
609 /* `\` at the end of the string: append `\` */
610 g_string_append_c(output
, *ch
);
615 for (es_char
= escapable_chars
; *es_char
!= '\0'; es_char
++) {
616 if (ch
[1] == *es_char
) {
618 * `\` followed by an escapable
619 * character: append the escaped
622 g_string_append_c(output
, ch
[1]);
624 continue_loop
= true;
634 * `\` followed by a non-escapable character:
635 * append `\` and the character.
637 g_string_append_c(output
, *ch
);
638 g_string_append_c(output
, ch
[1]);
642 for (end_char
= end_chars
; *end_char
!= '\0'; end_char
++) {
643 if (*ch
== *end_char
) {
645 * End character found:
646 * terminate this loop.
652 /* Normal character: append */
653 g_string_append_c(output
, *ch
);
659 *end_pos
= ch
- input
;
666 g_string_free(output
, TRUE
);
674 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
)
676 GString
*output
= g_string_new(NULL
);
678 bool no_quote
= true;
684 if (strlen(input
) == 0) {
685 if (with_single_quotes
) {
686 g_string_assign(output
, "''");
692 for (ch
= input
; *ch
!= '\0'; ch
++) {
695 if (!g_ascii_isalpha(c
) && !g_ascii_isdigit(c
) && c
!= '_' &&
696 c
!= '@' && c
!= '%' && c
!= '+' && c
!= '=' &&
697 c
!= ':' && c
!= ',' && c
!= '.' && c
!= '/' &&
705 g_string_assign(output
, input
);
709 if (with_single_quotes
) {
710 g_string_assign(output
, "'");
713 for (ch
= input
; *ch
!= '\0'; ch
++) {
715 g_string_append(output
, "'\"'\"'");
717 g_string_append_c(output
, *ch
);
721 if (with_single_quotes
) {
722 g_string_append_c(output
, '\'');
729 bool bt_common_string_is_printable(const char *input
)
732 bool printable
= true;
733 BT_ASSERT_DBG(input
);
735 for (ch
= input
; *ch
!= '\0'; ch
++) {
736 if (!isprint((unsigned char) *ch
) && *ch
!= '\n' && *ch
!= '\r' &&
737 *ch
!= '\t' && *ch
!= '\v') {
747 void bt_common_destroy_lttng_live_url_parts(
748 struct bt_common_lttng_live_url_parts
*parts
)
755 g_string_free(parts
->proto
, TRUE
);
759 if (parts
->hostname
) {
760 g_string_free(parts
->hostname
, TRUE
);
761 parts
->hostname
= NULL
;
764 if (parts
->target_hostname
) {
765 g_string_free(parts
->target_hostname
, TRUE
);
766 parts
->target_hostname
= NULL
;
769 if (parts
->session_name
) {
770 g_string_free(parts
->session_name
, TRUE
);
771 parts
->session_name
= NULL
;
778 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
779 const char *url
, char *error_buf
, size_t error_buf_size
)
781 struct bt_common_lttng_live_url_parts parts
;
782 const char *at
= url
;
786 memset(&parts
, 0, sizeof(parts
));
790 parts
.proto
= bt_common_string_until(at
, "", ":", &end_pos
);
791 if (!parts
.proto
|| parts
.proto
->len
== 0) {
793 snprintf(error_buf
, error_buf_size
, "Missing protocol");
799 if (strcmp(parts
.proto
->str
, "net") == 0) {
800 g_string_assign(parts
.proto
, "net4");
803 if (strcmp(parts
.proto
->str
, "net4") != 0 &&
804 strcmp(parts
.proto
->str
, "net6") != 0) {
806 snprintf(error_buf
, error_buf_size
,
807 "Unknown protocol: `%s`", parts
.proto
->str
);
813 if (at
[end_pos
] != ':') {
815 snprintf(error_buf
, error_buf_size
,
816 "Expecting `:` after `%s`", parts
.proto
->str
);
825 if (strncmp(at
, "://", 3) != 0) {
827 snprintf(error_buf
, error_buf_size
,
828 "Expecting `://` after protocol");
838 parts
.hostname
= bt_common_string_until(at
, "", ":/", &end_pos
);
839 if (!parts
.hostname
|| parts
.hostname
->len
== 0) {
841 snprintf(error_buf
, error_buf_size
, "Missing hostname");
847 if (at
[end_pos
] == ':') {
852 port
= bt_common_string_until(at
, "", "/", &end_pos
);
853 if (!port
|| port
->len
== 0) {
855 snprintf(error_buf
, error_buf_size
, "Missing port");
861 if (sscanf(port
->str
, "%d", &parts
.port
) != 1) {
863 snprintf(error_buf
, error_buf_size
,
864 "Invalid port: `%s`", port
->str
);
867 g_string_free(port
, TRUE
);
871 g_string_free(port
, TRUE
);
873 if (parts
.port
< 0 || parts
.port
>= 65536) {
875 snprintf(error_buf
, error_buf_size
,
876 "Invalid port: %d", parts
.port
);
883 if (at
[end_pos
] == '\0') {
884 /* Relay daemon hostname and ports provided only */
891 if (strncmp(at
, "/host/", 6) != 0) {
893 snprintf(error_buf
, error_buf_size
,
894 "Expecting `/host/` after hostname or port");
902 /* Target hostname */
903 parts
.target_hostname
= bt_common_string_until(at
, "", "/", &end_pos
);
904 if (!parts
.target_hostname
|| parts
.target_hostname
->len
== 0) {
906 snprintf(error_buf
, error_buf_size
,
907 "Missing target hostname");
913 if (at
[end_pos
] == '\0') {
915 snprintf(error_buf
, error_buf_size
,
916 "Missing `/` after target hostname (`%s`)",
917 parts
.target_hostname
->str
);
927 parts
.session_name
= bt_common_string_until(at
, "", "/", &end_pos
);
928 if (!parts
.session_name
|| parts
.session_name
->len
== 0) {
930 snprintf(error_buf
, error_buf_size
,
931 "Missing session name");
937 if (at
[end_pos
] == '/') {
939 snprintf(error_buf
, error_buf_size
,
940 "Unexpected `/` after session name (`%s`)",
941 parts
.session_name
->str
);
950 bt_common_destroy_lttng_live_url_parts(&parts
);
956 void bt_common_normalize_star_glob_pattern(char *pattern
)
960 bool got_star
= false;
964 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
968 /* Avoid consecutive stars. */
975 /* Copy backslash character. */
990 /* Copy single character. */
1000 bool at_end_of_pattern(const char *p
, const char *pattern
, size_t pattern_len
)
1002 return (p
- pattern
) == pattern_len
|| *p
== '\0';
1006 * Globbing matching function with the star feature only (`?` and
1007 * character sets are not supported). This matches `candidate` (plain
1008 * string) against `pattern`. A literal star can be escaped with `\` in
1011 * `pattern_len` or `candidate_len` can be greater than the actual
1012 * string length of `pattern` or `candidate` if the string is
1015 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
1016 const char *candidate
, size_t candidate_len
) {
1017 const char *retry_c
= candidate
, *retry_p
= pattern
, *c
, *p
;
1018 bool got_a_star
= false;
1025 * The concept here is to retry a match in the specific case
1026 * where we already got a star. The retry position for the
1027 * pattern is just after the most recent star, and the retry
1028 * position for the candidate is the character following the
1029 * last try's first character.
1033 * candidate: hi ev every onyx one
1035 * pattern: hi*every*one
1038 * candidate: hi ev every onyx one
1040 * pattern: hi*every*one
1043 * candidate: hi ev every onyx one
1045 * pattern: hi*every*one
1048 * candidate: hi ev every onyx one
1050 * pattern: hi*every*one
1053 * candidate: hi ev every onyx one
1055 * pattern: hi*every*one
1058 * candidate: hi ev every onyx one
1060 * pattern: hi*every*one
1063 * candidate: hi ev every onyx one
1065 * pattern: hi*every*one
1068 * candidate: hi ev every onyx one
1070 * pattern: hi*every*one
1073 * candidate: hi ev every onyx one
1075 * pattern: hi*every*one
1078 * candidate: hi ev every onyx one
1080 * pattern: hi*every*one
1083 * candidate: hi ev every onyx one
1085 * pattern: hi*every*one
1088 * candidate: hi ev every onyx one
1090 * pattern: hi*every*one
1093 * candidate: hi ev every onyx one
1095 * pattern: hi*every*one
1098 * candidate: hi ev every onyx one
1100 * pattern: hi*every*one
1103 * candidate: hi ev every onyx one
1105 * pattern: hi*every*one
1108 * candidate: hi ev every onyx one
1110 * pattern: hi*every*one
1113 * candidate: hi ev every onyx one
1115 * pattern: hi*every*one
1118 * candidate: hi ev every onyx one
1120 * pattern: hi*every*one
1123 * candidate: hi ev every onyx one
1125 * pattern: hi*every*one
1128 * candidate: hi ev every onyx one
1130 * pattern: hi*every*one
1133 * candidate: hi ev every onyx one
1135 * pattern: hi*every*one
1138 * candidate: hi ev every onyx one
1140 * pattern: hi*every*one
1143 * candidate: hi ev every onyx one
1145 * pattern: hi*every*one
1148 * candidate: hi ev every onyx one
1150 * pattern: hi*every*one
1153 * candidate: hi ev every onyx one
1155 * pattern: hi*every*one
1158 * candidate: hi ev every onyx one
1160 * pattern: hi*every*one
1163 * candidate: hi ev every onyx one
1165 * pattern: hi*every*one
1168 while ((c
- candidate
) < candidate_len
&& *c
!= '\0') {
1171 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1172 goto end_of_pattern
;
1180 * Our first try starts at the current candidate
1181 * character and after the star in the pattern.
1186 if (at_end_of_pattern(retry_p
, pattern
, pattern_len
)) {
1188 * Star at the end of the pattern at
1189 * this point: automatic match.
1196 /* Go to escaped character. */
1200 * Fall through the default case which compares
1201 * the escaped character now.
1205 if (at_end_of_pattern(p
, pattern
, pattern_len
) ||
1208 /* Character mismatch OR end of pattern. */
1211 * We didn't get any star yet,
1212 * so this first mismatch
1213 * automatically makes the whole
1220 * Next try: next candidate character,
1221 * original pattern character (following
1222 * the most recent star).
1230 /* Next pattern and candidate characters. */
1236 * We checked every candidate character and we're still in a
1237 * success state: the only pattern character allowed to remain
1240 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1245 return p
[-1] == '*' && at_end_of_pattern(p
, pattern
, pattern_len
);
1249 GString
*bt_common_normalize_path(const char *path
,
1250 const char *wd
__attribute__((unused
)))
1253 GString
*norm_path
= NULL
;
1257 tmp
= _fullpath(NULL
, path
, PATH_MAX
);
1262 norm_path
= g_string_new(tmp
);
1270 g_string_free(norm_path
, TRUE
);
1279 void append_path_parts(const char *path
, GPtrArray
*parts
)
1281 const char *ch
= path
;
1282 const char *last
= path
;
1285 if (*ch
== G_DIR_SEPARATOR
|| *ch
== '\0') {
1286 if (ch
- last
> 0) {
1287 GString
*part
= g_string_new(NULL
);
1290 g_string_append_len(part
, last
, ch
- last
);
1291 g_ptr_array_add(parts
, part
);
1306 void destroy_gstring(void *gstring
)
1308 (void) g_string_free(gstring
, TRUE
);
1311 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1315 GPtrArray
*parts
= NULL
;
1318 norm_path
= g_string_new(G_DIR_SEPARATOR_S
);
1323 parts
= g_ptr_array_new_with_free_func(destroy_gstring
);
1328 if (path
[0] != G_DIR_SEPARATOR
) {
1329 /* Relative path: start with working directory */
1331 append_path_parts(wd
, parts
);
1333 gchar
*cd
= g_get_current_dir();
1335 append_path_parts(cd
, parts
);
1340 /* Append parts of the path parameter */
1341 append_path_parts(path
, parts
);
1343 /* Resolve special `..` and `.` parts */
1344 for (i
= 0; i
< parts
->len
; i
++) {
1345 GString
*part
= g_ptr_array_index(parts
, i
);
1347 if (strcmp(part
->str
, "..") == 0) {
1350 * First part of absolute path is `..`:
1356 /* Remove `..` and previous part */
1357 g_ptr_array_remove_index(parts
, i
- 1);
1358 g_ptr_array_remove_index(parts
, i
- 1);
1360 } else if (strcmp(part
->str
, ".") == 0) {
1362 g_ptr_array_remove_index(parts
, i
);
1367 /* Create normalized path with what's left */
1368 for (i
= 0; i
< parts
->len
; i
++) {
1369 GString
*part
= g_ptr_array_index(parts
, i
);
1371 g_string_append(norm_path
, part
->str
);
1373 if (i
< parts
->len
- 1) {
1374 g_string_append_c(norm_path
, G_DIR_SEPARATOR
);
1382 g_string_free(norm_path
, TRUE
);
1388 g_ptr_array_free(parts
, TRUE
);
1395 size_t bt_common_get_page_size(int log_level
)
1399 page_size
= bt_sysconf(_SC_PAGESIZE
);
1400 if (page_size
< 0) {
1401 BT_LOGF("Cannot get system's page size: ret=%d",
1409 #define BUF_STD_APPEND(...) \
1411 char _tmp_fmt[64]; \
1413 size_t _size = buf_size - (size_t) (*buf_ch - buf); \
1414 size_t _tmp_fmt_size = (size_t) (fmt_ch - *out_fmt_ch); \
1415 strncpy(_tmp_fmt, *out_fmt_ch, _tmp_fmt_size); \
1416 _tmp_fmt[_tmp_fmt_size] = '\0'; \
1417 _Pragma("GCC diagnostic push") \
1418 _Pragma("GCC diagnostic ignored \"-Wformat-nonliteral\"") \
1419 _count = snprintf(*buf_ch, _size, _tmp_fmt, __VA_ARGS__); \
1420 _Pragma("GCC diagnostic pop") \
1421 BT_ASSERT_DBG(_count >= 0); \
1422 *buf_ch += MIN(_count, _size); \
1425 #define BUF_STD_APPEND_SINGLE_ARG(_type) \
1427 _type _arg = va_arg(*args, _type); \
1428 BUF_STD_APPEND(_arg); \
1431 static inline void handle_conversion_specifier_std(char *buf
, char **buf_ch
,
1432 size_t buf_size
, const char **out_fmt_ch
, va_list *args
)
1434 const char *fmt_ch
= *out_fmt_ch
;
1435 enum LENGTH_MODIFIER
{
1443 } length_mod
= LENGTH_MOD_NONE
;
1448 if (*fmt_ch
== '%') {
1474 if (*fmt_ch
< '0' || *fmt_ch
> '9') {
1482 if (*fmt_ch
== '.') {
1486 if (*fmt_ch
< '0' || *fmt_ch
> '9') {
1494 /* format (PRI*64) */
1495 if (strncmp(fmt_ch
, PRId64
, sizeof(PRId64
) - 1) == 0) {
1496 fmt_ch
+= sizeof(PRId64
) - 1;
1497 BUF_STD_APPEND_SINGLE_ARG(int64_t);
1499 } else if (strncmp(fmt_ch
, PRIu64
, sizeof(PRIu64
) - 1) == 0) {
1500 fmt_ch
+= sizeof(PRIu64
) - 1;
1501 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1503 } else if (strncmp(fmt_ch
, PRIx64
, sizeof(PRIx64
) - 1) == 0) {
1504 fmt_ch
+= sizeof(PRIx64
) - 1;
1505 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1507 } else if (strncmp(fmt_ch
, PRIX64
, sizeof(PRIX64
) - 1) == 0) {
1508 fmt_ch
+= sizeof(PRIX64
) - 1;
1509 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1511 } else if (strncmp(fmt_ch
, PRIo64
, sizeof(PRIo64
) - 1) == 0) {
1512 fmt_ch
+= sizeof(PRIo64
) - 1;
1513 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1515 } else if (strncmp(fmt_ch
, PRIi64
, sizeof(PRIi64
) - 1) == 0) {
1516 fmt_ch
+= sizeof(PRIi64
) - 1;
1517 BUF_STD_APPEND_SINGLE_ARG(int64_t);
1524 length_mod
= LENGTH_MOD_H
;
1527 if (*fmt_ch
== 'h') {
1528 length_mod
= LENGTH_MOD_HH
;
1534 length_mod
= LENGTH_MOD_LOW_L
;
1537 if (*fmt_ch
== 'l') {
1538 length_mod
= LENGTH_MOD_LOW_LL
;
1544 length_mod
= LENGTH_MOD_UP_L
;
1548 length_mod
= LENGTH_MOD_Z
;
1561 switch (length_mod
) {
1562 case LENGTH_MOD_NONE
:
1563 case LENGTH_MOD_LOW_L
:
1564 BUF_STD_APPEND_SINGLE_ARG(int);
1574 switch (length_mod
) {
1575 case LENGTH_MOD_NONE
:
1576 BUF_STD_APPEND_SINGLE_ARG(char *);
1578 case LENGTH_MOD_LOW_L
:
1579 BUF_STD_APPEND_SINGLE_ARG(wchar_t *);
1589 switch (length_mod
) {
1590 case LENGTH_MOD_NONE
:
1593 BUF_STD_APPEND_SINGLE_ARG(int);
1595 case LENGTH_MOD_LOW_L
:
1596 BUF_STD_APPEND_SINGLE_ARG(long);
1598 case LENGTH_MOD_LOW_LL
:
1599 BUF_STD_APPEND_SINGLE_ARG(long long);
1602 BUF_STD_APPEND_SINGLE_ARG(size_t);
1614 switch (length_mod
) {
1615 case LENGTH_MOD_NONE
:
1618 BUF_STD_APPEND_SINGLE_ARG(unsigned int);
1620 case LENGTH_MOD_LOW_L
:
1621 BUF_STD_APPEND_SINGLE_ARG(unsigned long);
1623 case LENGTH_MOD_LOW_LL
:
1624 BUF_STD_APPEND_SINGLE_ARG(unsigned long long);
1627 BUF_STD_APPEND_SINGLE_ARG(size_t);
1641 switch (length_mod
) {
1642 case LENGTH_MOD_NONE
:
1643 BUF_STD_APPEND_SINGLE_ARG(double);
1645 case LENGTH_MOD_UP_L
:
1646 BUF_STD_APPEND_SINGLE_ARG(long double);
1655 if (length_mod
== LENGTH_MOD_NONE
) {
1656 BUF_STD_APPEND_SINGLE_ARG(void *);
1666 *out_fmt_ch
= fmt_ch
;
1669 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
1671 bt_common_handle_custom_specifier_func handle_specifier
,
1672 void *priv_data
, const char *fmt
, va_list *args
)
1674 const char *fmt_ch
= fmt
;
1680 while (*fmt_ch
!= '\0') {
1683 BT_ASSERT_DBG(fmt_ch
[1] != '\0');
1685 if (fmt_ch
[1] == intro
) {
1686 handle_specifier(priv_data
, &buf_ch
,
1687 buf_size
- (size_t) (buf_ch
- buf
),
1690 handle_conversion_specifier_std(buf
, &buf_ch
,
1691 buf_size
, &fmt_ch
, args
);
1694 if (buf_ch
>= buf
+ buf_size
- 1) {
1701 if (buf_ch
>= buf
+ buf_size
- 1) {
1712 void bt_common_sep_digits(char *str
, unsigned int digits_per_group
, char sep
)
1721 BT_ASSERT_DBG(digits_per_group
> 0);
1722 BT_ASSERT_DBG(sep
!= '\0');
1724 /* Compute new length of `str` */
1725 orig_len
= strlen(str
);
1726 BT_ASSERT_DBG(orig_len
> 0);
1727 sep_count
= (orig_len
- 1) / digits_per_group
;
1728 new_len
= strlen(str
) + sep_count
;
1731 * Do the work in place. Have the reading pointer `rd` start at
1732 * the end of the original string, and the writing pointer `wr`
1733 * start at the end of the new string, making sure to also put a
1734 * null character there.
1736 rd
= str
+ orig_len
- 1;
1742 * Here's what the process looks like (3 digits per group):
1746 * Destination: 12345678#8
1751 * Destination: 1234567878
1756 * Destination: 1234567678
1761 * Destination: 123456,678
1766 * Destination: 123455,678
1771 * Destination: 123445,678
1776 * Destination: 123345,678
1781 * Destination: 12,345,678
1786 * Destination: 12,345,678
1791 * Destination: 12,345,678
1794 while (rd
!= str
- 1) {
1795 if (i
== digits_per_group
) {
1797 * Time to append the separator: decrement `wr`,
1798 * but keep `rd` as is.
1806 /* Copy read-side character to write-side character */
1814 GString
*bt_common_fold(const char *str
, unsigned int total_length
,
1815 unsigned int indent
)
1817 const unsigned int content_length
= total_length
- indent
;
1818 GString
*folded
= g_string_new(NULL
);
1819 GString
*tmp_line
= g_string_new(NULL
);
1820 gchar
**lines
= NULL
;
1821 gchar
**line_words
= NULL
;
1822 gchar
* const *line
;
1826 BT_ASSERT_DBG(indent
< total_length
);
1827 BT_ASSERT_DBG(tmp_line
);
1828 BT_ASSERT_DBG(folded
);
1830 if (strlen(str
) == 0) {
1831 /* Empty input string: empty output string */
1836 lines
= g_strsplit(str
, "\n", 0);
1837 BT_ASSERT_DBG(lines
);
1839 /* For each source line */
1840 for (line
= lines
; *line
; line
++) {
1841 gchar
* const *word
;
1844 * Append empty line without indenting if source line is
1847 if (strlen(*line
) == 0) {
1848 g_string_append_c(folded
, '\n');
1853 line_words
= g_strsplit(*line
, " ", 0);
1854 BT_ASSERT_DBG(line_words
);
1857 * Indent for first line (we know there's at least one
1858 * word at this point).
1860 for (i
= 0; i
< indent
; i
++) {
1861 g_string_append_c(folded
, ' ');
1864 /* Append words, folding when necessary */
1865 g_string_assign(tmp_line
, "");
1867 for (word
= line_words
; *word
; word
++) {
1869 * `tmp_line->len > 0` is in the condition so
1870 * that words that are larger than
1871 * `content_length` are at least written on
1874 * `tmp_line->len - 1` because the temporary
1875 * line always contains a trailing space which
1876 * won't be part of the line if we fold.
1878 if (tmp_line
->len
> 0 &&
1879 tmp_line
->len
- 1 + strlen(*word
) >= content_length
) {
1880 /* Fold (without trailing space) */
1881 g_string_append_len(folded
,
1882 tmp_line
->str
, tmp_line
->len
- 1);
1883 g_string_append_c(folded
, '\n');
1885 /* Indent new line */
1886 for (i
= 0; i
< indent
; i
++) {
1887 g_string_append_c(folded
, ' ');
1890 g_string_assign(tmp_line
, "");
1893 /* Append current word and space to temporary line */
1894 g_string_append(tmp_line
, *word
);
1895 g_string_append_c(tmp_line
, ' ');
1898 /* Append last line if any, without trailing space */
1899 if (tmp_line
->len
> 0) {
1900 g_string_append_len(folded
, tmp_line
->str
,
1904 /* Append source newline */
1905 g_string_append_c(folded
, '\n');
1907 /* Free array of this line's words */
1908 g_strfreev(line_words
);
1912 /* Remove trailing newline if any */
1913 if (folded
->str
[folded
->len
- 1] == '\n') {
1914 g_string_truncate(folded
, folded
->len
- 1);
1922 BT_ASSERT_DBG(!line_words
);
1925 g_string_free(tmp_line
, TRUE
);
1932 int bt_common_get_term_size(unsigned int *width
__attribute__((unused
)),
1933 unsigned int *height
__attribute__((unused
)))
1935 /* Not supported on Windows yet */
1938 #else /* __MINGW32__ */
1939 int bt_common_get_term_size(unsigned int *width
, unsigned int *height
)
1942 struct winsize winsize
;
1944 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &winsize
) < 0) {
1950 *width
= (unsigned int) winsize
.ws_col
;
1954 *height
= (unsigned int) winsize
.ws_row
;
1960 #endif /* __MINGW32__ */
1962 int bt_common_g_string_append_printf(GString
*str
, const char *fmt
, ...)
1965 gsize len
, allocated_len
, available_len
;
1968 /* str->len excludes \0. */
1970 /* Explicitly exclude \0. */
1971 allocated_len
= str
->allocated_len
- 1;
1972 available_len
= allocated_len
- len
;
1974 str
->len
= allocated_len
;
1976 print_len
= vsnprintf(str
->str
+ len
, available_len
+ 1, fmt
, ap
);
1978 if (print_len
< 0) {
1981 if (G_UNLIKELY(available_len
< print_len
)) {
1983 g_string_set_size(str
, len
+ print_len
);
1985 print_len
= vsnprintf(str
->str
+ len
, print_len
+ 1, fmt
, ap
);
1988 str
->len
= len
+ print_len
;
1993 int bt_common_append_file_content_to_g_string(GString
*str
, FILE *fp
)
1995 const size_t chunk_size
= 4096;
1999 gsize orig_len
= str
->len
;
2003 buf
= g_malloc(chunk_size
);
2019 read_len
= fread(buf
, 1, chunk_size
, fp
);
2020 g_string_append_len(str
, buf
, read_len
);
2025 /* Remove what was appended */
2026 g_string_truncate(str
, orig_len
);
2033 void bt_common_abort(void)
2035 static const char * const exec_on_abort_env_name
=
2036 "BABELTRACE_EXEC_ON_ABORT";
2037 const char *env_exec_on_abort
;
2039 env_exec_on_abort
= getenv(exec_on_abort_env_name
);
2040 if (env_exec_on_abort
) {
2041 if (bt_common_is_setuid_setgid()) {
2045 (void) g_spawn_command_line_sync(env_exec_on_abort
,
2046 NULL
, NULL
, NULL
, NULL
);