Commit | Line | Data |
---|---|---|
1670bffd PP |
1 | #ifndef BABELTRACE_COMMON_INTERNAL_H |
2 | #define BABELTRACE_COMMON_INTERNAL_H | |
3 | ||
c55a9f58 | 4 | #include <stdbool.h> |
8deee039 | 5 | #include <babeltrace/assert-internal.h> |
1670bffd | 6 | #include <babeltrace/babeltrace-internal.h> |
939190b3 | 7 | #include <babeltrace/trace-ir/field-classes.h> |
108b91d0 PP |
8 | #include <babeltrace/trace-ir/field-path.h> |
9 | #include <babeltrace/trace-ir/event-class.h> | |
17582c6d | 10 | #include <babeltrace/values.h> |
2be44e90 | 11 | #include <stdarg.h> |
7b33a0e0 PP |
12 | #include <inttypes.h> |
13 | #include <stdint.h> | |
8deee039 | 14 | #include <glib.h> |
1670bffd | 15 | |
ad96d936 PP |
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" | |
34 | ||
db0f160a PP |
35 | struct bt_common_lttng_live_url_parts { |
36 | GString *proto; | |
37 | GString *hostname; | |
38 | GString *target_hostname; | |
39 | GString *session_name; | |
40 | ||
41 | /* -1 means default port */ | |
42 | int port; | |
43 | }; | |
44 | ||
6eecdc5f PP |
45 | /* |
46 | * Checks if the current process has setuid or setgid access rights. | |
47 | * Returns `true` if so. | |
48 | */ | |
1670bffd PP |
49 | BT_HIDDEN |
50 | bool bt_common_is_setuid_setgid(void); | |
51 | ||
6eecdc5f PP |
52 | /* |
53 | * Returns the system-wide plugin path, e.g. | |
54 | * `/usr/lib/babeltrace/plugins`. Do not free the return value. | |
55 | */ | |
1670bffd PP |
56 | BT_HIDDEN |
57 | const char *bt_common_get_system_plugin_path(void); | |
58 | ||
6eecdc5f PP |
59 | /* |
60 | * Returns the user plugin path, e.g. | |
61 | * `/home/user/.local/lib/babeltrace/plugins`. You need to free the | |
62 | * return value. | |
63 | */ | |
1670bffd PP |
64 | BT_HIDDEN |
65 | char *bt_common_get_home_plugin_path(void); | |
66 | ||
6eecdc5f PP |
67 | /* |
68 | * Appends the list of directories in `paths` to the array `dirs`. | |
69 | * `paths` is a list of directories separated by `:`. Returns 0 on | |
70 | * success. | |
71 | */ | |
1670bffd PP |
72 | BT_HIDDEN |
73 | int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs); | |
74 | ||
6eecdc5f PP |
75 | /* |
76 | * Returns `true` if terminal color codes are supported for this | |
77 | * process. | |
78 | */ | |
ad96d936 PP |
79 | BT_HIDDEN |
80 | bool bt_common_colors_supported(void); | |
81 | ||
290725f7 PP |
82 | BT_HIDDEN |
83 | const char *bt_common_color_reset(void); | |
84 | ||
85 | BT_HIDDEN | |
86 | const char *bt_common_color_bold(void); | |
87 | ||
88 | BT_HIDDEN | |
89 | const char *bt_common_color_fg_default(void); | |
90 | ||
91 | BT_HIDDEN | |
92 | const char *bt_common_color_fg_red(void); | |
93 | ||
94 | BT_HIDDEN | |
95 | const char *bt_common_color_fg_green(void); | |
96 | ||
97 | BT_HIDDEN | |
98 | const char *bt_common_color_fg_yellow(void); | |
99 | ||
100 | BT_HIDDEN | |
101 | const char *bt_common_color_fg_blue(void); | |
102 | ||
103 | BT_HIDDEN | |
104 | const char *bt_common_color_fg_magenta(void); | |
105 | ||
106 | BT_HIDDEN | |
107 | const char *bt_common_color_fg_cyan(void); | |
108 | ||
109 | BT_HIDDEN | |
110 | const char *bt_common_color_fg_light_gray(void); | |
111 | ||
112 | BT_HIDDEN | |
113 | const char *bt_common_color_bg_default(void); | |
114 | ||
115 | BT_HIDDEN | |
116 | const char *bt_common_color_bg_red(void); | |
117 | ||
118 | BT_HIDDEN | |
119 | const char *bt_common_color_bg_green(void); | |
120 | ||
121 | BT_HIDDEN | |
122 | const char *bt_common_color_bg_yellow(void); | |
123 | ||
124 | BT_HIDDEN | |
125 | const char *bt_common_color_bg_blue(void); | |
126 | ||
127 | BT_HIDDEN | |
128 | const char *bt_common_color_bg_magenta(void); | |
129 | ||
130 | BT_HIDDEN | |
131 | const char *bt_common_color_bg_cyan(void); | |
132 | ||
133 | BT_HIDDEN | |
134 | const char *bt_common_color_bg_light_gray(void); | |
135 | ||
6eecdc5f PP |
136 | /* |
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. | |
141 | */ | |
db0f160a PP |
142 | BT_HIDDEN |
143 | GString *bt_common_string_until(const char *input, const char *escapable_chars, | |
144 | const char *end_chars, size_t *end_pos); | |
145 | ||
6eecdc5f PP |
146 | /* |
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. | |
152 | */ | |
db0f160a | 153 | BT_HIDDEN |
36b405c6 | 154 | GString *bt_common_shell_quote(const char *input, bool with_single_quotes); |
db0f160a | 155 | |
6eecdc5f PP |
156 | /* |
157 | * Returns `true` if `input` is a string made only of printable | |
158 | * characters. | |
159 | */ | |
db0f160a PP |
160 | BT_HIDDEN |
161 | bool bt_common_string_is_printable(const char *input); | |
162 | ||
6eecdc5f PP |
163 | /* |
164 | * Destroys the parts of an LTTng live URL as returned by | |
165 | * bt_common_parse_lttng_live_url(). | |
166 | */ | |
db0f160a PP |
167 | BT_HIDDEN |
168 | void bt_common_destroy_lttng_live_url_parts( | |
169 | struct bt_common_lttng_live_url_parts *parts); | |
170 | ||
6eecdc5f PP |
171 | /* |
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(). | |
176 | */ | |
db0f160a PP |
177 | BT_HIDDEN |
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); | |
180 | ||
6eecdc5f PP |
181 | /* |
182 | * Normalizes (in place) a star globbing pattern to be used with | |
183 | * bt_common_star_glob_match(). This function always succeeds. | |
184 | */ | |
9009cc24 PP |
185 | BT_HIDDEN |
186 | void bt_common_normalize_star_glob_pattern(char *pattern); | |
187 | ||
6eecdc5f PP |
188 | /* |
189 | * Returns `true` if `candidate` (of size `candidate_len`) matches | |
190 | * the star globbing pattern `pattern` (of size `pattern_len`). | |
191 | */ | |
9009cc24 PP |
192 | BT_HIDDEN |
193 | bool bt_common_star_glob_match(const char *pattern, size_t pattern_len, | |
194 | const char *candidate, size_t candidate_len); | |
195 | ||
6eecdc5f PP |
196 | /* |
197 | * Normalizes the path `path`: | |
198 | * | |
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 | |
201 | * if `wd` is NULL). | |
202 | * * Removes consecutive and trailing slashes. | |
203 | * * Resolves `..` and `.` in the path (both in `path` and in `wd`). | |
204 | * * Does NOT resolve symbolic links. | |
205 | * | |
206 | * The caller owns the returned GString. | |
207 | */ | |
e49a18d1 PP |
208 | BT_HIDDEN |
209 | GString *bt_common_normalize_path(const char *path, const char *wd); | |
210 | ||
2be44e90 PP |
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); | |
213 | ||
214 | /* | |
215 | * This is a custom vsnprintf() which handles the standard conversion | |
216 | * specifier as well as custom ones. | |
217 | * | |
218 | * `fmt` is a typical printf()-style format string, with the following | |
219 | * limitations: | |
220 | * | |
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 | |
227 | * `PRIi64`. | |
228 | * | |
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. | |
233 | * | |
234 | * When a custom conversion specifier is encountered in `fmt`, | |
235 | * the function calls `handle_specifier`. This callback receives: | |
236 | * | |
237 | * `priv_data`: | |
238 | * Custom, private data. | |
239 | * | |
240 | * `buf`: | |
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(). | |
245 | * | |
246 | * `avail_size`: | |
247 | * Number of bytes left in whole buffer from the `*buf` point. | |
248 | * | |
249 | * `fmt`: | |
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. | |
254 | * | |
255 | * `args`: | |
256 | * Variable argument list. Use va_arg() to get new arguments from | |
257 | * this list and update it at the same time. | |
258 | * | |
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). | |
262 | */ | |
263 | BT_HIDDEN | |
264 | void bt_common_custom_vsnprintf(char *buf, size_t buf_size, | |
265 | char intro, | |
266 | bt_common_handle_custom_specifier_func handle_specifier, | |
267 | void *priv_data, const char *fmt, va_list *args); | |
268 | ||
269 | /* | |
270 | * Variadic form of bt_common_custom_vsnprintf(). | |
271 | */ | |
272 | BT_HIDDEN | |
273 | void bt_common_custom_snprintf(char *buf, size_t buf_size, | |
274 | char intro, | |
275 | bt_common_handle_custom_specifier_func handle_specifier, | |
276 | void *priv_data, const char *fmt, ...); | |
277 | ||
108e5a1e | 278 | /* |
2be44e90 | 279 | * Returns the system page size. |
108e5a1e MJ |
280 | */ |
281 | BT_HIDDEN | |
282 | size_t bt_common_get_page_size(void); | |
283 | ||
8deee039 | 284 | static inline |
af0c18e3 | 285 | const char *bt_common_field_class_type_string(enum bt_field_class_type class_type) |
8deee039 | 286 | { |
af0c18e3 PP |
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"; | |
8deee039 PP |
308 | default: |
309 | return "(unknown)"; | |
310 | } | |
311 | }; | |
312 | ||
313 | static inline | |
939190b3 | 314 | const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base) |
8deee039 PP |
315 | { |
316 | switch (base) { | |
939190b3 PP |
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"; | |
8deee039 PP |
325 | default: |
326 | return "(unknown)"; | |
327 | } | |
328 | } | |
329 | ||
330 | static inline | |
331 | const char *bt_common_scope_string(enum bt_scope scope) | |
332 | { | |
333 | switch (scope) { | |
7b33a0e0 PP |
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"; | |
8deee039 PP |
344 | case BT_SCOPE_EVENT_PAYLOAD: |
345 | return "BT_SCOPE_EVENT_PAYLOAD"; | |
8deee039 PP |
346 | default: |
347 | return "(unknown)"; | |
348 | } | |
349 | } | |
350 | ||
351 | static inline | |
352 | const char *bt_common_event_class_log_level_string( | |
353 | enum bt_event_class_log_level level) | |
354 | { | |
355 | switch (level) { | |
8deee039 PP |
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"; | |
386 | default: | |
387 | return "(unknown)"; | |
388 | } | |
389 | }; | |
390 | ||
17582c6d PP |
391 | static inline |
392 | const char *bt_common_value_type_string(enum bt_value_type type) | |
393 | { | |
394 | switch (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"; | |
409 | default: | |
410 | return "(unknown)"; | |
411 | } | |
412 | }; | |
413 | ||
8deee039 PP |
414 | static inline |
415 | GString *bt_field_path_string(struct bt_field_path *path) | |
416 | { | |
417 | GString *str = g_string_new(NULL); | |
7b33a0e0 | 418 | uint64_t i; |
8deee039 PP |
419 | |
420 | BT_ASSERT(path); | |
421 | ||
422 | if (!str) { | |
423 | goto end; | |
424 | } | |
425 | ||
426 | g_string_append_printf(str, "[%s", bt_common_scope_string( | |
427 | bt_field_path_get_root_scope(path))); | |
428 | ||
429 | for (i = 0; i < bt_field_path_get_index_count(path); i++) { | |
7b33a0e0 PP |
430 | g_string_append_printf(str, ", %" PRIu64, |
431 | bt_field_path_get_index_by_index(path, i)); | |
8deee039 PP |
432 | } |
433 | ||
434 | g_string_append(str, "]"); | |
435 | ||
436 | end: | |
437 | return str; | |
438 | } | |
439 | ||
1670bffd | 440 | #endif /* BABELTRACE_COMMON_INTERNAL_H */ |