1 #ifndef BABELTRACE_COMMON_INTERNAL_H
2 #define BABELTRACE_COMMON_INTERNAL_H
5 * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation
6 * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include <babeltrace/assert-internal.h>
29 #include <babeltrace/babeltrace-internal.h>
30 #include <babeltrace/trace-ir/field-classes-const.h>
31 #include <babeltrace/trace-ir/field-path-const.h>
32 #include <babeltrace/trace-ir/event-class-const.h>
33 #include <babeltrace/values.h>
39 #define BT_COMMON_COLOR_RESET "\033[0m"
40 #define BT_COMMON_COLOR_BOLD "\033[1m"
41 #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
42 #define BT_COMMON_COLOR_FG_RED "\033[31m"
43 #define BT_COMMON_COLOR_FG_GREEN "\033[32m"
44 #define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
45 #define BT_COMMON_COLOR_FG_BLUE "\033[34m"
46 #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
47 #define BT_COMMON_COLOR_FG_CYAN "\033[36m"
48 #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
49 #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
50 #define BT_COMMON_COLOR_BG_RED "\033[41m"
51 #define BT_COMMON_COLOR_BG_GREEN "\033[42m"
52 #define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
53 #define BT_COMMON_COLOR_BG_BLUE "\033[44m"
54 #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
55 #define BT_COMMON_COLOR_BG_CYAN "\033[46m"
56 #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
58 struct bt_common_lttng_live_url_parts
{
61 GString
*target_hostname
;
62 GString
*session_name
;
64 /* -1 means default port */
69 * Checks if the current process has setuid or setgid access rights.
70 * Returns `true` if so.
73 bool bt_common_is_setuid_setgid(void);
76 * Returns the system-wide plugin path, e.g.
77 * `/usr/lib/babeltrace/plugins`. Do not free the return value.
80 const char *bt_common_get_system_plugin_path(void);
83 * Returns the user plugin path, e.g.
84 * `/home/user/.local/lib/babeltrace/plugins`. You need to free the
88 char *bt_common_get_home_plugin_path(void);
91 * Appends the list of directories in `paths` to the array `dirs`.
92 * `paths` is a list of directories separated by `:`. Returns 0 on
96 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
);
99 * Returns `true` if terminal color codes are supported for this
103 bool bt_common_colors_supported(void);
106 const char *bt_common_color_reset(void);
109 const char *bt_common_color_bold(void);
112 const char *bt_common_color_fg_default(void);
115 const char *bt_common_color_fg_red(void);
118 const char *bt_common_color_fg_green(void);
121 const char *bt_common_color_fg_yellow(void);
124 const char *bt_common_color_fg_blue(void);
127 const char *bt_common_color_fg_magenta(void);
130 const char *bt_common_color_fg_cyan(void);
133 const char *bt_common_color_fg_light_gray(void);
136 const char *bt_common_color_bg_default(void);
139 const char *bt_common_color_bg_red(void);
142 const char *bt_common_color_bg_green(void);
145 const char *bt_common_color_bg_yellow(void);
148 const char *bt_common_color_bg_blue(void);
151 const char *bt_common_color_bg_magenta(void);
154 const char *bt_common_color_bg_cyan(void);
157 const char *bt_common_color_bg_light_gray(void);
160 * Returns the substring from `input` to the first character found
161 * in the list of characters `end_chars`, unescaping any character
162 * found in `escapable_chars`, and sets `*end_pos` to the position of
163 * the end (from `input`). The caller owns the returned GString.
166 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
167 const char *end_chars
, size_t *end_pos
);
170 * Returns the quoted version of `input` for a shell. If
171 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
172 * and suffix to the returned string; otherwise the caller should
173 * prepend and append them manually, although they are not always
174 * required. The caller owns the returned GString.
177 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
);
180 * Returns `true` if `input` is a string made only of printable
184 bool bt_common_string_is_printable(const char *input
);
187 * Destroys the parts of an LTTng live URL as returned by
188 * bt_common_parse_lttng_live_url().
191 void bt_common_destroy_lttng_live_url_parts(
192 struct bt_common_lttng_live_url_parts
*parts
);
195 * Parses the LTTng live URL `url` and returns its different parts.
196 * If there's an error, writes the error message into `*error_buf`
197 * up to `error_buf_size` bytes. You must destroy the returned value
198 * with bt_common_destroy_lttng_live_url_parts().
201 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
202 const char *url
, char *error_buf
, size_t error_buf_size
);
205 * Normalizes (in place) a star globbing pattern to be used with
206 * bt_common_star_glob_match(). This function always succeeds.
209 void bt_common_normalize_star_glob_pattern(char *pattern
);
212 * Returns `true` if `candidate` (of size `candidate_len`) matches
213 * the star globbing pattern `pattern` (of size `pattern_len`).
216 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
217 const char *candidate
, size_t candidate_len
);
220 * Normalizes the path `path`:
222 * * If it's a relative path, converts it to an absolute path using
223 * `wd` as the working directory (or the current working directory
225 * * Removes consecutive and trailing slashes.
226 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
227 * * Does NOT resolve symbolic links.
229 * The caller owns the returned GString.
232 GString
*bt_common_normalize_path(const char *path
, const char *wd
);
234 typedef void (* bt_common_handle_custom_specifier_func
)(void *priv_data
,
235 char **buf
, size_t avail_size
, const char **fmt
, va_list *args
);
238 * This is a custom vsnprintf() which handles the standard conversion
239 * specifier as well as custom ones.
241 * `fmt` is a typical printf()-style format string, with the following
244 * * The `*` width specifier is not accepted.
245 * * The `*` precision specifier is not accepted.
246 * * The `j` and `t` length modifiers are not accepted.
247 * * The `n` format specifier is not accepted.
248 * * The format specifiers defined in <inttypes.h> are not accepted
249 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
252 * `intro` specifies which special character immediately following an
253 * introductory `%` character in `fmt` is used to indicate a custom
254 * conversion specifier. For example, if `intro` is '@', then any `%@`
255 * sequence in `fmt` is the beginning of a custom conversion specifier.
257 * When a custom conversion specifier is encountered in `fmt`,
258 * the function calls `handle_specifier`. This callback receives:
261 * Custom, private data.
264 * Address of the current buffer pointer. `*buf` is the position to
265 * append new data. The callback must update `*buf` when appending
266 * new data. The callback must ensure not to write passed the whole
267 * buffer passed to bt_common_custom_vsnprintf().
270 * Number of bytes left in whole buffer from the `*buf` point.
273 * Address of the current format string pointer. `*fmt` points to
274 * the introductory `%` character, which is followed by the
275 * character `intro`. The callback must update `*fmt` so that it
276 * points after the whole custom conversion specifier.
279 * Variable argument list. Use va_arg() to get new arguments from
280 * this list and update it at the same time.
282 * Because this is an internal utility, this function and its callback
283 * do not return error codes: they abort when there's any error (bad
284 * format string, for example).
287 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
289 bt_common_handle_custom_specifier_func handle_specifier
,
290 void *priv_data
, const char *fmt
, va_list *args
);
293 * Variadic form of bt_common_custom_vsnprintf().
296 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
298 bt_common_handle_custom_specifier_func handle_specifier
,
299 void *priv_data
, const char *fmt
, ...);
302 * Returns the system page size.
305 size_t bt_common_get_page_size(void);
308 const char *bt_common_field_class_type_string(enum bt_field_class_type class_type
)
310 switch (class_type
) {
311 case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER
:
312 return "BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER";
313 case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER
:
314 return "BT_FIELD_CLASS_TYPE_SIGNED_INTEGER";
315 case BT_FIELD_CLASS_TYPE_REAL
:
316 return "BT_FIELD_CLASS_TYPE_REAL";
317 case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION
:
318 return "BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION";
319 case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION
:
320 return "BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION";
321 case BT_FIELD_CLASS_TYPE_STRING
:
322 return "BT_FIELD_CLASS_TYPE_STRING";
323 case BT_FIELD_CLASS_TYPE_STRUCTURE
:
324 return "BT_FIELD_CLASS_TYPE_STRUCTURE";
325 case BT_FIELD_CLASS_TYPE_STATIC_ARRAY
:
326 return "BT_FIELD_CLASS_TYPE_STATIC_ARRAY";
327 case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY
:
328 return "BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY";
329 case BT_FIELD_CLASS_TYPE_VARIANT
:
330 return "BT_FIELD_CLASS_TYPE_VARIANT";
337 const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base
)
340 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY
:
341 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
342 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL
:
343 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
344 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL
:
345 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
346 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL
:
347 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
354 const char *bt_common_scope_string(enum bt_scope scope
)
357 case BT_SCOPE_PACKET_HEADER
:
358 return "BT_SCOPE_PACKET_HEADER";
359 case BT_SCOPE_PACKET_CONTEXT
:
360 return "BT_SCOPE_PACKET_CONTEXT";
361 case BT_SCOPE_EVENT_HEADER
:
362 return "BT_SCOPE_EVENT_HEADER";
363 case BT_SCOPE_EVENT_COMMON_CONTEXT
:
364 return "BT_SCOPE_EVENT_COMMON_CONTEXT";
365 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT
:
366 return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT";
367 case BT_SCOPE_EVENT_PAYLOAD
:
368 return "BT_SCOPE_EVENT_PAYLOAD";
375 const char *bt_common_event_class_log_level_string(
376 enum bt_event_class_log_level level
)
379 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY
:
380 return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY";
381 case BT_EVENT_CLASS_LOG_LEVEL_ALERT
:
382 return "BT_EVENT_CLASS_LOG_LEVEL_ALERT";
383 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL
:
384 return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL";
385 case BT_EVENT_CLASS_LOG_LEVEL_ERROR
:
386 return "BT_EVENT_CLASS_LOG_LEVEL_ERROR";
387 case BT_EVENT_CLASS_LOG_LEVEL_WARNING
:
388 return "BT_EVENT_CLASS_LOG_LEVEL_WARNING";
389 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE
:
390 return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE";
391 case BT_EVENT_CLASS_LOG_LEVEL_INFO
:
392 return "BT_EVENT_CLASS_LOG_LEVEL_INFO";
393 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM
:
394 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM";
395 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM
:
396 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM";
397 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS
:
398 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS";
399 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE
:
400 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE";
401 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT
:
402 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT";
403 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION
:
404 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION";
405 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE
:
406 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE";
407 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG
:
408 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG";
415 const char *bt_common_value_type_string(enum bt_value_type type
)
418 case BT_VALUE_TYPE_NULL
:
419 return "BT_VALUE_TYPE_NULL";
420 case BT_VALUE_TYPE_BOOL
:
421 return "BT_VALUE_TYPE_BOOL";
422 case BT_VALUE_TYPE_INTEGER
:
423 return "BT_VALUE_TYPE_INTEGER";
424 case BT_VALUE_TYPE_REAL
:
425 return "BT_VALUE_TYPE_REAL";
426 case BT_VALUE_TYPE_STRING
:
427 return "BT_VALUE_TYPE_STRING";
428 case BT_VALUE_TYPE_ARRAY
:
429 return "BT_VALUE_TYPE_ARRAY";
430 case BT_VALUE_TYPE_MAP
:
431 return "BT_VALUE_TYPE_MAP";
438 GString
*bt_field_path_string(struct bt_field_path
*path
)
440 GString
*str
= g_string_new(NULL
);
449 g_string_append_printf(str
, "[%s", bt_common_scope_string(
450 bt_field_path_get_root_scope(path
)));
452 for (i
= 0; i
< bt_field_path_get_index_count(path
); i
++) {
453 g_string_append_printf(str
, ", %" PRIu64
,
454 bt_field_path_get_index_by_index(path
, i
));
457 g_string_append(str
, "]");
463 #endif /* BABELTRACE_COMMON_INTERNAL_H */