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/trace-ir/field-classes.h>
8 #include <babeltrace/trace-ir/field-path.h>
9 #include <babeltrace/trace-ir/event-class.h>
10 #include <babeltrace/values.h>
16 #define BT_COMMON_COLOR_RESET "\033[0m"
17 #define BT_COMMON_COLOR_BOLD "\033[1m"
18 #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
19 #define BT_COMMON_COLOR_FG_RED "\033[31m"
20 #define BT_COMMON_COLOR_FG_GREEN "\033[32m"
21 #define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
22 #define BT_COMMON_COLOR_FG_BLUE "\033[34m"
23 #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
24 #define BT_COMMON_COLOR_FG_CYAN "\033[36m"
25 #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
26 #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
27 #define BT_COMMON_COLOR_BG_RED "\033[41m"
28 #define BT_COMMON_COLOR_BG_GREEN "\033[42m"
29 #define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
30 #define BT_COMMON_COLOR_BG_BLUE "\033[44m"
31 #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
32 #define BT_COMMON_COLOR_BG_CYAN "\033[46m"
33 #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
35 struct bt_common_lttng_live_url_parts
{
38 GString
*target_hostname
;
39 GString
*session_name
;
41 /* -1 means default port */
46 * Checks if the current process has setuid or setgid access rights.
47 * Returns `true` if so.
50 bool bt_common_is_setuid_setgid(void);
53 * Returns the system-wide plugin path, e.g.
54 * `/usr/lib/babeltrace/plugins`. Do not free the return value.
57 const char *bt_common_get_system_plugin_path(void);
60 * Returns the user plugin path, e.g.
61 * `/home/user/.local/lib/babeltrace/plugins`. You need to free the
65 char *bt_common_get_home_plugin_path(void);
68 * Appends the list of directories in `paths` to the array `dirs`.
69 * `paths` is a list of directories separated by `:`. Returns 0 on
73 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
);
76 * Returns `true` if terminal color codes are supported for this
80 bool bt_common_colors_supported(void);
83 const char *bt_common_color_reset(void);
86 const char *bt_common_color_bold(void);
89 const char *bt_common_color_fg_default(void);
92 const char *bt_common_color_fg_red(void);
95 const char *bt_common_color_fg_green(void);
98 const char *bt_common_color_fg_yellow(void);
101 const char *bt_common_color_fg_blue(void);
104 const char *bt_common_color_fg_magenta(void);
107 const char *bt_common_color_fg_cyan(void);
110 const char *bt_common_color_fg_light_gray(void);
113 const char *bt_common_color_bg_default(void);
116 const char *bt_common_color_bg_red(void);
119 const char *bt_common_color_bg_green(void);
122 const char *bt_common_color_bg_yellow(void);
125 const char *bt_common_color_bg_blue(void);
128 const char *bt_common_color_bg_magenta(void);
131 const char *bt_common_color_bg_cyan(void);
134 const char *bt_common_color_bg_light_gray(void);
137 * Returns the substring from `input` to the first character found
138 * in the list of characters `end_chars`, unescaping any character
139 * found in `escapable_chars`, and sets `*end_pos` to the position of
140 * the end (from `input`). The caller owns the returned GString.
143 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
144 const char *end_chars
, size_t *end_pos
);
147 * Returns the quoted version of `input` for a shell. If
148 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
149 * and suffix to the returned string; otherwise the caller should
150 * prepend and append them manually, although they are not always
151 * required. The caller owns the returned GString.
154 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
);
157 * Returns `true` if `input` is a string made only of printable
161 bool bt_common_string_is_printable(const char *input
);
164 * Destroys the parts of an LTTng live URL as returned by
165 * bt_common_parse_lttng_live_url().
168 void bt_common_destroy_lttng_live_url_parts(
169 struct bt_common_lttng_live_url_parts
*parts
);
172 * Parses the LTTng live URL `url` and returns its different parts.
173 * If there's an error, writes the error message into `*error_buf`
174 * up to `error_buf_size` bytes. You must destroy the returned value
175 * with bt_common_destroy_lttng_live_url_parts().
178 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
179 const char *url
, char *error_buf
, size_t error_buf_size
);
182 * Normalizes (in place) a star globbing pattern to be used with
183 * bt_common_star_glob_match(). This function always succeeds.
186 void bt_common_normalize_star_glob_pattern(char *pattern
);
189 * Returns `true` if `candidate` (of size `candidate_len`) matches
190 * the star globbing pattern `pattern` (of size `pattern_len`).
193 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
194 const char *candidate
, size_t candidate_len
);
197 * Normalizes the path `path`:
199 * * If it's a relative path, converts it to an absolute path using
200 * `wd` as the working directory (or the current working directory
202 * * Removes consecutive and trailing slashes.
203 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
204 * * Does NOT resolve symbolic links.
206 * The caller owns the returned GString.
209 GString
*bt_common_normalize_path(const char *path
, const char *wd
);
211 typedef void (* bt_common_handle_custom_specifier_func
)(void *priv_data
,
212 char **buf
, size_t avail_size
, const char **fmt
, va_list *args
);
215 * This is a custom vsnprintf() which handles the standard conversion
216 * specifier as well as custom ones.
218 * `fmt` is a typical printf()-style format string, with the following
221 * * The `*` width specifier is not accepted.
222 * * The `*` precision specifier is not accepted.
223 * * The `j` and `t` length modifiers are not accepted.
224 * * The `n` format specifier is not accepted.
225 * * The format specifiers defined in <inttypes.h> are not accepted
226 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
229 * `intro` specifies which special character immediately following an
230 * introductory `%` character in `fmt` is used to indicate a custom
231 * conversion specifier. For example, if `intro` is '@', then any `%@`
232 * sequence in `fmt` is the beginning of a custom conversion specifier.
234 * When a custom conversion specifier is encountered in `fmt`,
235 * the function calls `handle_specifier`. This callback receives:
238 * Custom, private data.
241 * Address of the current buffer pointer. `*buf` is the position to
242 * append new data. The callback must update `*buf` when appending
243 * new data. The callback must ensure not to write passed the whole
244 * buffer passed to bt_common_custom_vsnprintf().
247 * Number of bytes left in whole buffer from the `*buf` point.
250 * Address of the current format string pointer. `*fmt` points to
251 * the introductory `%` character, which is followed by the
252 * character `intro`. The callback must update `*fmt` so that it
253 * points after the whole custom conversion specifier.
256 * Variable argument list. Use va_arg() to get new arguments from
257 * this list and update it at the same time.
259 * Because this is an internal utility, this function and its callback
260 * do not return error codes: they abort when there's any error (bad
261 * format string, for example).
264 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
266 bt_common_handle_custom_specifier_func handle_specifier
,
267 void *priv_data
, const char *fmt
, va_list *args
);
270 * Variadic form of bt_common_custom_vsnprintf().
273 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
275 bt_common_handle_custom_specifier_func handle_specifier
,
276 void *priv_data
, const char *fmt
, ...);
279 * Returns the system page size.
282 size_t bt_common_get_page_size(void);
285 const char *bt_common_field_class_type_string(enum bt_field_class_type class_type
)
287 switch (class_type
) {
288 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
289 return "BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER";
290 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
291 return "BT_FIELD_CLASS_TYPE_SIGNED_INTEGER";
292 case BT_FIELD_CLASS_TYPE_REAL
:
293 return "BT_FIELD_CLASS_TYPE_REAL";
294 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
295 return "BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION";
296 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
297 return "BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION";
298 case BT_FIELD_CLASS_TYPE_STRING
:
299 return "BT_FIELD_CLASS_TYPE_STRING";
300 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
301 return "BT_FIELD_CLASS_TYPE_STRUCTURE";
302 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
303 return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY";
304 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
305 return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY";
306 case BT_FIELD_CLASS_TYPE_VARIANT
:
307 return "BT_FIELD_CLASS_TYPE_VARIANT";
314 const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base
)
317 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
318 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
319 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
320 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
321 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
322 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
323 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
324 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
331 const char *bt_common_scope_string(enum bt_scope scope
)
334 case BT_SCOPE_PACKET_HEADER
:
335 return "BT_SCOPE_PACKET_HEADER";
336 case BT_SCOPE_PACKET_CONTEXT
:
337 return "BT_SCOPE_PACKET_CONTEXT";
338 case BT_SCOPE_EVENT_HEADER
:
339 return "BT_SCOPE_EVENT_HEADER";
340 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
341 return "BT_SCOPE_EVENT_COMMON_CONTEXT";
342 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
343 return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT";
344 case BT_SCOPE_EVENT_PAYLOAD
:
345 return "BT_SCOPE_EVENT_PAYLOAD";
352 const char *bt_common_event_class_log_level_string(
353 enum bt_event_class_log_level level
)
356 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
:
357 return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY";
358 case BT_EVENT_CLASS_LOG_LEVEL_ALERT
:
359 return "BT_EVENT_CLASS_LOG_LEVEL_ALERT";
360 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
:
361 return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL";
362 case BT_EVENT_CLASS_LOG_LEVEL_ERROR
:
363 return "BT_EVENT_CLASS_LOG_LEVEL_ERROR";
364 case BT_EVENT_CLASS_LOG_LEVEL_WARNING
:
365 return "BT_EVENT_CLASS_LOG_LEVEL_WARNING";
366 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE
:
367 return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE";
368 case BT_EVENT_CLASS_LOG_LEVEL_INFO
:
369 return "BT_EVENT_CLASS_LOG_LEVEL_INFO";
370 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
:
371 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM";
372 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
:
373 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM";
374 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
:
375 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS";
376 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
:
377 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE";
378 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
:
379 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT";
380 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
:
381 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION";
382 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
:
383 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE";
384 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG
:
385 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG";
392 const char *bt_common_value_type_string(enum bt_value_type type
)
395 case BT_VALUE_TYPE_NULL
:
396 return "BT_VALUE_TYPE_NULL";
397 case BT_VALUE_TYPE_BOOL
:
398 return "BT_VALUE_TYPE_BOOL";
399 case BT_VALUE_TYPE_INTEGER
:
400 return "BT_VALUE_TYPE_INTEGER";
401 case BT_VALUE_TYPE_REAL
:
402 return "BT_VALUE_TYPE_REAL";
403 case BT_VALUE_TYPE_STRING
:
404 return "BT_VALUE_TYPE_STRING";
405 case BT_VALUE_TYPE_ARRAY
:
406 return "BT_VALUE_TYPE_ARRAY";
407 case BT_VALUE_TYPE_MAP
:
408 return "BT_VALUE_TYPE_MAP";
415 GString
*bt_field_path_string(struct bt_field_path
*path
)
417 GString
*str
= g_string_new(NULL
);
426 g_string_append_printf(str
, "[%s", bt_common_scope_string(
427 bt_field_path_get_root_scope(path
)));
429 for (i
= 0; i
< bt_field_path_get_index_count(path
); i
++) {
430 g_string_append_printf(str
, ", %" PRIu64
,
431 bt_field_path_get_index_by_index(path
, i
));
434 g_string_append(str
, "]");
440 #endif /* BABELTRACE_COMMON_INTERNAL_H */