1 #ifndef BABELTRACE_COMMON_INTERNAL_H
2 #define BABELTRACE_COMMON_INTERNAL_H
5 #include <babeltrace/assert-internal.h>
6 #include <babeltrace/babeltrace-internal.h>
7 #include <babeltrace/ctf-ir/field-types.h>
8 #include <babeltrace/ctf-ir/field-path.h>
9 #include <babeltrace/ctf-ir/event-class.h>
15 #define BT_COMMON_COLOR_RESET "\033[0m"
16 #define BT_COMMON_COLOR_BOLD "\033[1m"
17 #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
18 #define BT_COMMON_COLOR_FG_RED "\033[31m"
19 #define BT_COMMON_COLOR_FG_GREEN "\033[32m"
20 #define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
21 #define BT_COMMON_COLOR_FG_BLUE "\033[34m"
22 #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
23 #define BT_COMMON_COLOR_FG_CYAN "\033[36m"
24 #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
25 #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
26 #define BT_COMMON_COLOR_BG_RED "\033[41m"
27 #define BT_COMMON_COLOR_BG_GREEN "\033[42m"
28 #define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
29 #define BT_COMMON_COLOR_BG_BLUE "\033[44m"
30 #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
31 #define BT_COMMON_COLOR_BG_CYAN "\033[46m"
32 #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
34 struct bt_common_lttng_live_url_parts
{
37 GString
*target_hostname
;
38 GString
*session_name
;
40 /* -1 means default port */
45 * Checks if the current process has setuid or setgid access rights.
46 * Returns `true` if so.
49 bool bt_common_is_setuid_setgid(void);
52 * Returns the system-wide plugin path, e.g.
53 * `/usr/lib/babeltrace/plugins`. Do not free the return value.
56 const char *bt_common_get_system_plugin_path(void);
59 * Returns the user plugin path, e.g.
60 * `/home/user/.local/lib/babeltrace/plugins`. You need to free the
64 char *bt_common_get_home_plugin_path(void);
67 * Appends the list of directories in `paths` to the array `dirs`.
68 * `paths` is a list of directories separated by `:`. Returns 0 on
72 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
);
75 * Returns `true` if terminal color codes are supported for this
79 bool bt_common_colors_supported(void);
82 const char *bt_common_color_reset(void);
85 const char *bt_common_color_bold(void);
88 const char *bt_common_color_fg_default(void);
91 const char *bt_common_color_fg_red(void);
94 const char *bt_common_color_fg_green(void);
97 const char *bt_common_color_fg_yellow(void);
100 const char *bt_common_color_fg_blue(void);
103 const char *bt_common_color_fg_magenta(void);
106 const char *bt_common_color_fg_cyan(void);
109 const char *bt_common_color_fg_light_gray(void);
112 const char *bt_common_color_bg_default(void);
115 const char *bt_common_color_bg_red(void);
118 const char *bt_common_color_bg_green(void);
121 const char *bt_common_color_bg_yellow(void);
124 const char *bt_common_color_bg_blue(void);
127 const char *bt_common_color_bg_magenta(void);
130 const char *bt_common_color_bg_cyan(void);
133 const char *bt_common_color_bg_light_gray(void);
136 * Returns the substring from `input` to the first character found
137 * in the list of characters `end_chars`, unescaping any character
138 * found in `escapable_chars`, and sets `*end_pos` to the position of
139 * the end (from `input`). The caller owns the returned GString.
142 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
143 const char *end_chars
, size_t *end_pos
);
146 * Returns the quoted version of `input` for a shell. If
147 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
148 * and suffix to the returned string; otherwise the caller should
149 * prepend and append them manually, although they are not always
150 * required. The caller owns the returned GString.
153 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
);
156 * Returns `true` if `input` is a string made only of printable
160 bool bt_common_string_is_printable(const char *input
);
163 * Destroys the parts of an LTTng live URL as returned by
164 * bt_common_parse_lttng_live_url().
167 void bt_common_destroy_lttng_live_url_parts(
168 struct bt_common_lttng_live_url_parts
*parts
);
171 * Parses the LTTng live URL `url` and returns its different parts.
172 * If there's an error, writes the error message into `*error_buf`
173 * up to `error_buf_size` bytes. You must destroy the returned value
174 * with bt_common_destroy_lttng_live_url_parts().
177 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
178 const char *url
, char *error_buf
, size_t error_buf_size
);
181 * Normalizes (in place) a star globbing pattern to be used with
182 * bt_common_star_glob_match(). This function always succeeds.
185 void bt_common_normalize_star_glob_pattern(char *pattern
);
188 * Returns `true` if `candidate` (of size `candidate_len`) matches
189 * the star globbing pattern `pattern` (of size `pattern_len`).
192 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
193 const char *candidate
, size_t candidate_len
);
196 * Normalizes the path `path`:
198 * * If it's a relative path, converts it to an absolute path using
199 * `wd` as the working directory (or the current working directory
201 * * Removes consecutive and trailing slashes.
202 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
203 * * Does NOT resolve symbolic links.
205 * The caller owns the returned GString.
208 GString
*bt_common_normalize_path(const char *path
, const char *wd
);
210 typedef void (* bt_common_handle_custom_specifier_func
)(void *priv_data
,
211 char **buf
, size_t avail_size
, const char **fmt
, va_list *args
);
214 * This is a custom vsnprintf() which handles the standard conversion
215 * specifier as well as custom ones.
217 * `fmt` is a typical printf()-style format string, with the following
220 * * The `*` width specifier is not accepted.
221 * * The `*` precision specifier is not accepted.
222 * * The `j` and `t` length modifiers are not accepted.
223 * * The `n` format specifier is not accepted.
224 * * The format specifiers defined in <inttypes.h> are not accepted
225 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
228 * `intro` specifies which special character immediately following an
229 * introductory `%` character in `fmt` is used to indicate a custom
230 * conversion specifier. For example, if `intro` is '@', then any `%@`
231 * sequence in `fmt` is the beginning of a custom conversion specifier.
233 * When a custom conversion specifier is encountered in `fmt`,
234 * the function calls `handle_specifier`. This callback receives:
237 * Custom, private data.
240 * Address of the current buffer pointer. `*buf` is the position to
241 * append new data. The callback must update `*buf` when appending
242 * new data. The callback must ensure not to write passed the whole
243 * buffer passed to bt_common_custom_vsnprintf().
246 * Number of bytes left in whole buffer from the `*buf` point.
249 * Address of the current format string pointer. `*fmt` points to
250 * the introductory `%` character, which is followed by the
251 * character `intro`. The callback must update `*fmt` so that it
252 * points after the whole custom conversion specifier.
255 * Variable argument list. Use va_arg() to get new arguments from
256 * this list and update it at the same time.
258 * Because this is an internal utility, this function and its callback
259 * do not return error codes: they abort when there's any error (bad
260 * format string, for example).
263 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
265 bt_common_handle_custom_specifier_func handle_specifier
,
266 void *priv_data
, const char *fmt
, va_list *args
);
269 * Variadic form of bt_common_custom_vsnprintf().
272 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
274 bt_common_handle_custom_specifier_func handle_specifier
,
275 void *priv_data
, const char *fmt
, ...);
278 * Returns the system page size.
281 size_t bt_common_get_page_size(void);
284 const char *bt_common_field_type_id_string(enum bt_field_type_id type_id
)
287 case BT_FIELD_TYPE_ID_UNSIGNED_INTEGER
:
288 return "BT_FIELD_TYPE_ID_UNSIGNED_INTEGER";
289 case BT_FIELD_TYPE_ID_SIGNED_INTEGER
:
290 return "BT_FIELD_TYPE_ID_SIGNED_INTEGER";
291 case BT_FIELD_TYPE_ID_REAL
:
292 return "BT_FIELD_TYPE_ID_REAL";
293 case BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION
:
294 return "BT_FIELD_TYPE_ID_UNSIGNED_ENUMERATION";
295 case BT_FIELD_TYPE_ID_SIGNED_ENUMERATION
:
296 return "BT_FIELD_TYPE_ID_SIGNED_ENUMERATION";
297 case BT_FIELD_TYPE_ID_STRING
:
298 return "BT_FIELD_TYPE_ID_STRING";
299 case BT_FIELD_TYPE_ID_STRUCTURE
:
300 return "BT_FIELD_TYPE_ID_STRUCTURE";
301 case BT_FIELD_TYPE_ID_STATIC_ARRAY
:
302 return "BT_FIELD_TYPE_ID_STATIC_ARRAY";
303 case BT_FIELD_TYPE_ID_DYNAMIC_ARRAY
:
304 return "BT_FIELD_TYPE_ID_DYNAMIC_ARRAY";
305 case BT_FIELD_TYPE_ID_VARIANT
:
306 return "BT_FIELD_TYPE_ID_VARIANT";
313 const char *bt_common_field_type_integer_preferred_display_base_string(enum bt_field_type_integer_preferred_display_base base
)
316 case BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
317 return "BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
318 case BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
319 return "BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
320 case BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
321 return "BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
322 case BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
323 return "BT_FIELD_TYPE_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
330 const char *bt_common_scope_string(enum bt_scope scope
)
333 case BT_SCOPE_PACKET_HEADER
:
334 return "BT_SCOPE_PACKET_HEADER";
335 case BT_SCOPE_PACKET_CONTEXT
:
336 return "BT_SCOPE_PACKET_CONTEXT";
337 case BT_SCOPE_EVENT_HEADER
:
338 return "BT_SCOPE_EVENT_HEADER";
339 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
340 return "BT_SCOPE_EVENT_COMMON_CONTEXT";
341 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
342 return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT";
343 case BT_SCOPE_EVENT_PAYLOAD
:
344 return "BT_SCOPE_EVENT_PAYLOAD";
351 const char *bt_common_event_class_log_level_string(
352 enum bt_event_class_log_level level
)
355 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
:
356 return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY";
357 case BT_EVENT_CLASS_LOG_LEVEL_ALERT
:
358 return "BT_EVENT_CLASS_LOG_LEVEL_ALERT";
359 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
:
360 return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL";
361 case BT_EVENT_CLASS_LOG_LEVEL_ERROR
:
362 return "BT_EVENT_CLASS_LOG_LEVEL_ERROR";
363 case BT_EVENT_CLASS_LOG_LEVEL_WARNING
:
364 return "BT_EVENT_CLASS_LOG_LEVEL_WARNING";
365 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE
:
366 return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE";
367 case BT_EVENT_CLASS_LOG_LEVEL_INFO
:
368 return "BT_EVENT_CLASS_LOG_LEVEL_INFO";
369 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
:
370 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM";
371 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
:
372 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM";
373 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
:
374 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS";
375 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
:
376 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE";
377 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
:
378 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT";
379 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
:
380 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION";
381 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
:
382 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE";
383 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG
:
384 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG";
391 GString
*bt_field_path_string(struct bt_field_path
*path
)
393 GString
*str
= g_string_new(NULL
);
402 g_string_append_printf(str
, "[%s", bt_common_scope_string(
403 bt_field_path_get_root_scope(path
)));
405 for (i
= 0; i
< bt_field_path_get_index_count(path
); i
++) {
406 g_string_append_printf(str
, ", %" PRIu64
,
407 bt_field_path_get_index_by_index(path
, i
));
410 g_string_append(str
, "]");
416 #endif /* BABELTRACE_COMMON_INTERNAL_H */