Commit | Line | Data |
---|---|---|
d94d92ac | 1 | /* |
0235b0db MJ |
2 | * SPDX-License-Identifier: MIT |
3 | * | |
d94d92ac PP |
4 | * Copyright (c) 2018 EfficiOS Inc. and Linux Foundation |
5 | * Copyright (c) 2018 Philippe Proulx <pproulx@efficios.com> | |
d94d92ac PP |
6 | */ |
7 | ||
0235b0db MJ |
8 | #ifndef BABELTRACE_COMMON_INTERNAL_H |
9 | #define BABELTRACE_COMMON_INTERNAL_H | |
10 | ||
91d81473 MJ |
11 | #include <errno.h> |
12 | #include <glib.h> | |
44c440bc | 13 | #include <inttypes.h> |
282b829b | 14 | #include <stdarg.h> |
91d81473 | 15 | #include <stdbool.h> |
44c440bc | 16 | #include <stdint.h> |
91d81473 | 17 | #include <stdlib.h> |
87c88284 | 18 | #include <stdio.h> |
282b829b | 19 | #include <unistd.h> |
5ab3582b | 20 | #include <string.h> |
91d81473 | 21 | |
4fa90f32 | 22 | #include <babeltrace2/babeltrace.h> |
91d81473 | 23 | |
4fa90f32 | 24 | #define __BT_IN_BABELTRACE_H |
d24d5663 | 25 | #include <babeltrace2/func-status.h> |
d24d5663 | 26 | |
91d81473 MJ |
27 | #include "common/assert.h" |
28 | #include "common/macros.h" | |
29 | #include "common/safe.h" | |
1670bffd | 30 | |
97ebbaa0 PP |
31 | #ifdef __cplusplus |
32 | extern "C" { | |
33 | #endif | |
34 | ||
04609487 PP |
35 | #define BT_COMMON_COLOR_RESET "\033[0m" |
36 | #define BT_COMMON_COLOR_BOLD "\033[1m" | |
37 | #define BT_COMMON_COLOR_FG_DEFAULT "\033[39m" | |
38 | #define BT_COMMON_COLOR_FG_RED "\033[31m" | |
39 | #define BT_COMMON_COLOR_FG_GREEN "\033[32m" | |
40 | #define BT_COMMON_COLOR_FG_YELLOW "\033[33m" | |
41 | #define BT_COMMON_COLOR_FG_BLUE "\033[34m" | |
42 | #define BT_COMMON_COLOR_FG_MAGENTA "\033[35m" | |
43 | #define BT_COMMON_COLOR_FG_CYAN "\033[36m" | |
44 | #define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m" | |
45 | #define BT_COMMON_COLOR_FG_BOLD_RED "\033[1m\033[31m" | |
46 | #define BT_COMMON_COLOR_FG_BOLD_GREEN "\033[1m\033[32m" | |
47 | #define BT_COMMON_COLOR_FG_BOLD_YELLOW "\033[1m\033[33m" | |
48 | #define BT_COMMON_COLOR_FG_BOLD_BLUE "\033[1m\033[34m" | |
49 | #define BT_COMMON_COLOR_FG_BOLD_MAGENTA "\033[1m\033[35m" | |
50 | #define BT_COMMON_COLOR_FG_BOLD_CYAN "\033[1m\033[36m" | |
51 | #define BT_COMMON_COLOR_FG_BOLD_LIGHT_GRAY "\033[1m\033[37m" | |
52 | #define BT_COMMON_COLOR_FG_BRIGHT_RED "\033[91m" | |
53 | #define BT_COMMON_COLOR_FG_BRIGHT_GREEN "\033[92m" | |
54 | #define BT_COMMON_COLOR_FG_BRIGHT_YELLOW "\033[93m" | |
55 | #define BT_COMMON_COLOR_FG_BRIGHT_BLUE "\033[94m" | |
56 | #define BT_COMMON_COLOR_FG_BRIGHT_MAGENTA "\033[95m" | |
57 | #define BT_COMMON_COLOR_FG_BRIGHT_CYAN "\033[96m" | |
58 | #define BT_COMMON_COLOR_FG_BRIGHT_LIGHT_GRAY "\033[97m" | |
59 | #define BT_COMMON_COLOR_BG_DEFAULT "\033[49m" | |
60 | #define BT_COMMON_COLOR_BG_RED "\033[41m" | |
61 | #define BT_COMMON_COLOR_BG_GREEN "\033[42m" | |
62 | #define BT_COMMON_COLOR_BG_YELLOW "\033[43m" | |
63 | #define BT_COMMON_COLOR_BG_BLUE "\033[44m" | |
64 | #define BT_COMMON_COLOR_BG_MAGENTA "\033[45m" | |
65 | #define BT_COMMON_COLOR_BG_CYAN "\033[46m" | |
66 | #define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m" | |
ad96d936 | 67 | |
3aa68ad9 SM |
68 | enum bt_common_color_when { |
69 | BT_COMMON_COLOR_WHEN_AUTO, | |
70 | BT_COMMON_COLOR_WHEN_ALWAYS, | |
71 | BT_COMMON_COLOR_WHEN_NEVER, | |
72 | }; | |
73 | ||
74 | struct bt_common_color_codes { | |
75 | const char *reset; | |
76 | const char *bold; | |
77 | const char *fg_default; | |
78 | const char *fg_red; | |
79 | const char *fg_green; | |
80 | const char *fg_yellow; | |
81 | const char *fg_blue; | |
82 | const char *fg_magenta; | |
83 | const char *fg_cyan; | |
84 | const char *fg_light_gray; | |
85 | const char *fg_bright_red; | |
86 | const char *fg_bright_green; | |
87 | const char *fg_bright_yellow; | |
88 | const char *fg_bright_blue; | |
89 | const char *fg_bright_magenta; | |
90 | const char *fg_bright_cyan; | |
91 | const char *fg_bright_light_gray; | |
92 | const char *bg_default; | |
93 | const char *bg_red; | |
94 | const char *bg_green; | |
95 | const char *bg_yellow; | |
96 | const char *bg_blue; | |
97 | const char *bg_magenta; | |
98 | const char *bg_cyan; | |
99 | const char *bg_light_gray; | |
100 | }; | |
101 | ||
db0f160a PP |
102 | struct bt_common_lttng_live_url_parts { |
103 | GString *proto; | |
104 | GString *hostname; | |
105 | GString *target_hostname; | |
106 | GString *session_name; | |
107 | ||
108 | /* -1 means default port */ | |
109 | int port; | |
110 | }; | |
111 | ||
6eecdc5f PP |
112 | /* |
113 | * Checks if the current process has setuid or setgid access rights. | |
114 | * Returns `true` if so. | |
115 | */ | |
1670bffd PP |
116 | bool bt_common_is_setuid_setgid(void); |
117 | ||
6eecdc5f PP |
118 | /* |
119 | * Returns the system-wide plugin path, e.g. | |
d9676d8c | 120 | * `/usr/lib/babeltrace2/plugins`. Do not free the return value. |
6eecdc5f | 121 | */ |
1670bffd PP |
122 | const char *bt_common_get_system_plugin_path(void); |
123 | ||
6eecdc5f PP |
124 | /* |
125 | * Returns the user plugin path, e.g. | |
d9676d8c | 126 | * `/home/user/.local/lib/babeltrace2/plugins`. You need to free the |
6eecdc5f PP |
127 | * return value. |
128 | */ | |
86d8b7b8 | 129 | char *bt_common_get_home_plugin_path(int log_level); |
1670bffd | 130 | |
6eecdc5f PP |
131 | /* |
132 | * Appends the list of directories in `paths` to the array `dirs`. | |
133 | * `paths` is a list of directories separated by `:`. Returns 0 on | |
134 | * success. | |
135 | */ | |
1670bffd PP |
136 | int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs); |
137 | ||
6eecdc5f PP |
138 | /* |
139 | * Returns `true` if terminal color codes are supported for this | |
140 | * process. | |
141 | */ | |
ad96d936 PP |
142 | bool bt_common_colors_supported(void); |
143 | ||
290725f7 PP |
144 | const char *bt_common_color_reset(void); |
145 | ||
290725f7 PP |
146 | const char *bt_common_color_bold(void); |
147 | ||
290725f7 PP |
148 | const char *bt_common_color_fg_default(void); |
149 | ||
290725f7 PP |
150 | const char *bt_common_color_fg_red(void); |
151 | ||
290725f7 PP |
152 | const char *bt_common_color_fg_green(void); |
153 | ||
290725f7 PP |
154 | const char *bt_common_color_fg_yellow(void); |
155 | ||
290725f7 PP |
156 | const char *bt_common_color_fg_blue(void); |
157 | ||
290725f7 PP |
158 | const char *bt_common_color_fg_magenta(void); |
159 | ||
290725f7 PP |
160 | const char *bt_common_color_fg_cyan(void); |
161 | ||
290725f7 PP |
162 | const char *bt_common_color_fg_light_gray(void); |
163 | ||
04609487 PP |
164 | const char *bt_common_color_fg_bright_red(void); |
165 | ||
04609487 PP |
166 | const char *bt_common_color_fg_bright_green(void); |
167 | ||
04609487 PP |
168 | const char *bt_common_color_fg_bright_yellow(void); |
169 | ||
04609487 PP |
170 | const char *bt_common_color_fg_bright_blue(void); |
171 | ||
04609487 PP |
172 | const char *bt_common_color_fg_bright_magenta(void); |
173 | ||
04609487 PP |
174 | const char *bt_common_color_fg_bright_cyan(void); |
175 | ||
04609487 PP |
176 | const char *bt_common_color_fg_bright_light_gray(void); |
177 | ||
290725f7 PP |
178 | const char *bt_common_color_bg_default(void); |
179 | ||
290725f7 PP |
180 | const char *bt_common_color_bg_red(void); |
181 | ||
290725f7 PP |
182 | const char *bt_common_color_bg_green(void); |
183 | ||
290725f7 PP |
184 | const char *bt_common_color_bg_yellow(void); |
185 | ||
290725f7 PP |
186 | const char *bt_common_color_bg_blue(void); |
187 | ||
290725f7 PP |
188 | const char *bt_common_color_bg_magenta(void); |
189 | ||
290725f7 PP |
190 | const char *bt_common_color_bg_cyan(void); |
191 | ||
290725f7 PP |
192 | const char *bt_common_color_bg_light_gray(void); |
193 | ||
3aa68ad9 SM |
194 | void bt_common_color_get_codes(struct bt_common_color_codes *codes, |
195 | enum bt_common_color_when use_colors); | |
196 | ||
6eecdc5f PP |
197 | /* |
198 | * Returns the substring from `input` to the first character found | |
199 | * in the list of characters `end_chars`, unescaping any character | |
200 | * found in `escapable_chars`, and sets `*end_pos` to the position of | |
201 | * the end (from `input`). The caller owns the returned GString. | |
202 | */ | |
db0f160a PP |
203 | GString *bt_common_string_until(const char *input, const char *escapable_chars, |
204 | const char *end_chars, size_t *end_pos); | |
205 | ||
6eecdc5f PP |
206 | /* |
207 | * Returns the quoted version of `input` for a shell. If | |
208 | * `with_single_quotes` is `true`, prepends and appends the `'` prefix | |
209 | * and suffix to the returned string; otherwise the caller should | |
210 | * prepend and append them manually, although they are not always | |
211 | * required. The caller owns the returned GString. | |
212 | */ | |
36b405c6 | 213 | GString *bt_common_shell_quote(const char *input, bool with_single_quotes); |
db0f160a | 214 | |
6eecdc5f PP |
215 | /* |
216 | * Returns `true` if `input` is a string made only of printable | |
217 | * characters. | |
218 | */ | |
db0f160a PP |
219 | bool bt_common_string_is_printable(const char *input); |
220 | ||
6eecdc5f PP |
221 | /* |
222 | * Destroys the parts of an LTTng live URL as returned by | |
223 | * bt_common_parse_lttng_live_url(). | |
224 | */ | |
db0f160a PP |
225 | void bt_common_destroy_lttng_live_url_parts( |
226 | struct bt_common_lttng_live_url_parts *parts); | |
227 | ||
6eecdc5f PP |
228 | /* |
229 | * Parses the LTTng live URL `url` and returns its different parts. | |
230 | * If there's an error, writes the error message into `*error_buf` | |
231 | * up to `error_buf_size` bytes. You must destroy the returned value | |
232 | * with bt_common_destroy_lttng_live_url_parts(). | |
233 | */ | |
db0f160a PP |
234 | struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url( |
235 | const char *url, char *error_buf, size_t error_buf_size); | |
236 | ||
6eecdc5f PP |
237 | /* |
238 | * Normalizes (in place) a star globbing pattern to be used with | |
239 | * bt_common_star_glob_match(). This function always succeeds. | |
240 | */ | |
9009cc24 PP |
241 | void bt_common_normalize_star_glob_pattern(char *pattern); |
242 | ||
6eecdc5f PP |
243 | /* |
244 | * Returns `true` if `candidate` (of size `candidate_len`) matches | |
245 | * the star globbing pattern `pattern` (of size `pattern_len`). | |
246 | */ | |
9009cc24 PP |
247 | bool bt_common_star_glob_match(const char *pattern, size_t pattern_len, |
248 | const char *candidate, size_t candidate_len); | |
249 | ||
6eecdc5f PP |
250 | /* |
251 | * Normalizes the path `path`: | |
252 | * | |
253 | * * If it's a relative path, converts it to an absolute path using | |
254 | * `wd` as the working directory (or the current working directory | |
255 | * if `wd` is NULL). | |
256 | * * Removes consecutive and trailing slashes. | |
257 | * * Resolves `..` and `.` in the path (both in `path` and in `wd`). | |
258 | * * Does NOT resolve symbolic links. | |
259 | * | |
260 | * The caller owns the returned GString. | |
261 | */ | |
e49a18d1 PP |
262 | GString *bt_common_normalize_path(const char *path, const char *wd); |
263 | ||
85cd02cf PP |
264 | typedef void (* bt_common_handle_custom_specifier_func)(void *priv_data, |
265 | char **buf, size_t avail_size, const char **fmt, va_list *args); | |
266 | ||
267 | /* | |
268 | * This is a custom vsnprintf() which handles the standard conversion | |
269 | * specifier as well as custom ones. | |
270 | * | |
271 | * `fmt` is a typical printf()-style format string, with the following | |
272 | * limitations: | |
273 | * | |
274 | * * The `*` width specifier is not accepted. | |
275 | * * The `*` precision specifier is not accepted. | |
276 | * * The `j` and `t` length modifiers are not accepted. | |
277 | * * The `n` format specifier is not accepted. | |
278 | * * The format specifiers defined in <inttypes.h> are not accepted | |
279 | * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and | |
280 | * `PRIi64`. | |
281 | * | |
282 | * `intro` specifies which special character immediately following an | |
283 | * introductory `%` character in `fmt` is used to indicate a custom | |
284 | * conversion specifier. For example, if `intro` is '@', then any `%@` | |
285 | * sequence in `fmt` is the beginning of a custom conversion specifier. | |
286 | * | |
287 | * When a custom conversion specifier is encountered in `fmt`, | |
288 | * the function calls `handle_specifier`. This callback receives: | |
289 | * | |
290 | * `priv_data`: | |
291 | * Custom, private data. | |
292 | * | |
293 | * `buf`: | |
294 | * Address of the current buffer pointer. `*buf` is the position to | |
295 | * append new data. The callback must update `*buf` when appending | |
296 | * new data. The callback must ensure not to write passed the whole | |
297 | * buffer passed to bt_common_custom_vsnprintf(). | |
298 | * | |
299 | * `avail_size`: | |
300 | * Number of bytes left in whole buffer from the `*buf` point. | |
301 | * | |
302 | * `fmt`: | |
303 | * Address of the current format string pointer. `*fmt` points to | |
304 | * the introductory `%` character, which is followed by the | |
305 | * character `intro`. The callback must update `*fmt` so that it | |
306 | * points after the whole custom conversion specifier. | |
307 | * | |
308 | * `args`: | |
309 | * Variable argument list. Use va_arg() to get new arguments from | |
310 | * this list and update it at the same time. | |
311 | * | |
312 | * Because this is an internal utility, this function and its callback | |
313 | * do not return error codes: they abort when there's any error (bad | |
314 | * format string, for example). | |
315 | */ | |
85cd02cf PP |
316 | void bt_common_custom_vsnprintf(char *buf, size_t buf_size, |
317 | char intro, | |
318 | bt_common_handle_custom_specifier_func handle_specifier, | |
319 | void *priv_data, const char *fmt, va_list *args); | |
320 | ||
108e5a1e | 321 | /* |
85cd02cf | 322 | * Returns the system page size. |
108e5a1e | 323 | */ |
86d8b7b8 | 324 | size_t bt_common_get_page_size(int log_level); |
108e5a1e | 325 | |
ae83436e PP |
326 | /* |
327 | * Adds the digit separator `sep` as many times as needed to form groups | |
328 | * of `digits_per_group` digits within `str`. `str` must have enough | |
e7401568 | 329 | * room to accommodate the new separators, that is: |
ae83436e PP |
330 | * |
331 | * strlen(str) + (strlen(str) / digits_per_group) + 1 | |
332 | * | |
333 | * Example: with `str` `1983198398213`, `digits_per_group` 3, and `sep` | |
334 | * `,`, `str` becomes `1,983,198,398,213`. | |
335 | * | |
336 | * `strlen(str)` must not be 0. `digits_per_group` must not be 0. `sep` | |
337 | * must not be `\0`. | |
338 | */ | |
ae83436e PP |
339 | void bt_common_sep_digits(char *str, unsigned int digits_per_group, char sep); |
340 | ||
775d0390 PP |
341 | /* |
342 | * This is similar to what the command `fold --spaces` does: it wraps | |
343 | * the input lines of `str`, breaking at spaces, and indenting each line | |
344 | * with `indent` spaces so that each line fits the total length | |
345 | * `total_length`. | |
346 | * | |
347 | * If an original line in `str` contains a word which is >= the content | |
348 | * length (`total_length - indent`), then the corresponding folded line | |
349 | * is also larger than the content length. In other words, breaking at | |
350 | * spaces is a best effort, but it might not be possible. | |
351 | * | |
352 | * The returned string, on success, is owned by the caller. | |
353 | */ | |
775d0390 PP |
354 | GString *bt_common_fold(const char *str, unsigned int total_length, |
355 | unsigned int indent); | |
356 | ||
2c4f022e PP |
357 | /* |
358 | * Writes the terminal's width to `*width`, its height to `*height`, | |
359 | * and returns 0 on success, or returns -1 on error. | |
360 | */ | |
2c4f022e PP |
361 | int bt_common_get_term_size(unsigned int *width, unsigned int *height); |
362 | ||
87c88284 PP |
363 | /* |
364 | * Appends the textual content of `fp` to `str`, starting from its | |
365 | * current position to the end of the file. | |
366 | * | |
367 | * This function does NOT rewind `fp` once it's done or on error. | |
368 | */ | |
87c88284 PP |
369 | int bt_common_append_file_content_to_g_string(GString *str, FILE *fp); |
370 | ||
498e7994 PP |
371 | void bt_common_abort(void) __attribute__((noreturn)); |
372 | ||
3dca2276 | 373 | static inline |
864cad70 | 374 | const char *bt_common_field_class_type_string(enum bt_field_class_type class_type) |
3dca2276 | 375 | { |
864cad70 | 376 | switch (class_type) { |
0aa006b7 PP |
377 | case BT_FIELD_CLASS_TYPE_BOOL: |
378 | return "BOOL"; | |
379 | case BT_FIELD_CLASS_TYPE_BIT_ARRAY: | |
380 | return "BIT_ARRAY"; | |
00dff0ec SM |
381 | case BT_FIELD_CLASS_TYPE_INTEGER: |
382 | return "INTEGER"; | |
864cad70 | 383 | case BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER: |
8a432889 | 384 | return "UNSIGNED_INTEGER"; |
864cad70 | 385 | case BT_FIELD_CLASS_TYPE_SIGNED_INTEGER: |
8a432889 | 386 | return "SIGNED_INTEGER"; |
00dff0ec SM |
387 | case BT_FIELD_CLASS_TYPE_ENUMERATION: |
388 | return "ENUMERATION"; | |
864cad70 | 389 | case BT_FIELD_CLASS_TYPE_UNSIGNED_ENUMERATION: |
8a432889 | 390 | return "UNSIGNED_ENUMERATION"; |
864cad70 | 391 | case BT_FIELD_CLASS_TYPE_SIGNED_ENUMERATION: |
8a432889 | 392 | return "SIGNED_ENUMERATION"; |
00dff0ec SM |
393 | case BT_FIELD_CLASS_TYPE_REAL: |
394 | return "REAL"; | |
0aa006b7 PP |
395 | case BT_FIELD_CLASS_TYPE_SINGLE_PRECISION_REAL: |
396 | return "SINGLE_PRECISION_REAL"; | |
397 | case BT_FIELD_CLASS_TYPE_DOUBLE_PRECISION_REAL: | |
398 | return "DOUBLE_PRECISION_REAL"; | |
864cad70 | 399 | case BT_FIELD_CLASS_TYPE_STRING: |
8a432889 | 400 | return "STRING"; |
864cad70 | 401 | case BT_FIELD_CLASS_TYPE_STRUCTURE: |
8a432889 | 402 | return "STRUCTURE"; |
00dff0ec SM |
403 | case BT_FIELD_CLASS_TYPE_ARRAY: |
404 | return "ARRAY"; | |
864cad70 | 405 | case BT_FIELD_CLASS_TYPE_STATIC_ARRAY: |
8a432889 | 406 | return "STATIC_ARRAY"; |
00dff0ec SM |
407 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY: |
408 | return "DYNAMIC_ARRAY"; | |
81b8fa44 PP |
409 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD: |
410 | return "DYNAMIC_ARRAY_WITHOUT_LENGTH_FIELD"; | |
411 | case BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY_WITH_LENGTH_FIELD: | |
412 | return "DYNAMIC_ARRAY_WITH_LENGTH_FIELD"; | |
00dff0ec SM |
413 | case BT_FIELD_CLASS_TYPE_OPTION: |
414 | return "OPTION"; | |
de821fe5 PP |
415 | case BT_FIELD_CLASS_TYPE_OPTION_WITHOUT_SELECTOR_FIELD: |
416 | return "OPTION_WITHOUT_SELECTOR_FIELD"; | |
00dff0ec SM |
417 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_SELECTOR_FIELD: |
418 | return "OPTION_WITH_SELECTOR_FIELD"; | |
de821fe5 PP |
419 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_BOOL_SELECTOR_FIELD: |
420 | return "OPTION_WITH_BOOL_SELECTOR_FIELD"; | |
00dff0ec SM |
421 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_INTEGER_SELECTOR_FIELD: |
422 | return "OPTION_WITH_INTEGER_SELECTOR_FIELD"; | |
de821fe5 PP |
423 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: |
424 | return "OPTION_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD"; | |
425 | case BT_FIELD_CLASS_TYPE_OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD: | |
426 | return "OPTION_WITH_SIGNED_INTEGER_SELECTOR_FIELD"; | |
00dff0ec SM |
427 | case BT_FIELD_CLASS_TYPE_VARIANT: |
428 | return "VARIANT"; | |
de821fe5 PP |
429 | case BT_FIELD_CLASS_TYPE_VARIANT_WITHOUT_SELECTOR_FIELD: |
430 | return "VARIANT_WITHOUT_SELECTOR_FIELD"; | |
00dff0ec SM |
431 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SELECTOR_FIELD: |
432 | return "VARIANT_WITH_SELECTOR_FIELD"; | |
433 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_INTEGER_SELECTOR_FIELD: | |
434 | return "VARIANT_WITH_INTEGER_SELECTOR_FIELD"; | |
de821fe5 PP |
435 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD: |
436 | return "VARIANT_WITH_UNSIGNED_INTEGER_SELECTOR_FIELD"; | |
437 | case BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD: | |
438 | return "VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD"; | |
00dff0ec SM |
439 | case __BT_FIELD_CLASS_TYPE_BIG_VALUE: |
440 | bt_common_abort (); | |
3dca2276 | 441 | } |
00dff0ec SM |
442 | |
443 | bt_common_abort(); | |
3dca2276 PP |
444 | }; |
445 | ||
446 | static inline | |
5cd6d0e5 | 447 | const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base) |
3dca2276 PP |
448 | { |
449 | switch (base) { | |
5cd6d0e5 | 450 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY: |
8a432889 | 451 | return "BINARY"; |
5cd6d0e5 | 452 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL: |
8a432889 | 453 | return "OCTAL"; |
5cd6d0e5 | 454 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL: |
8a432889 | 455 | return "DECIMAL"; |
5cd6d0e5 | 456 | case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL: |
8a432889 | 457 | return "HEXADECIMAL"; |
3dca2276 | 458 | } |
00dff0ec SM |
459 | |
460 | bt_common_abort(); | |
3dca2276 PP |
461 | } |
462 | ||
463 | static inline | |
e7ceb9df | 464 | const char *bt_common_scope_string(enum bt_field_path_scope scope) |
3dca2276 PP |
465 | { |
466 | switch (scope) { | |
e7ceb9df | 467 | case BT_FIELD_PATH_SCOPE_PACKET_CONTEXT: |
8a432889 | 468 | return "PACKET_CONTEXT"; |
e7ceb9df | 469 | case BT_FIELD_PATH_SCOPE_EVENT_COMMON_CONTEXT: |
8a432889 | 470 | return "EVENT_COMMON_CONTEXT"; |
e7ceb9df | 471 | case BT_FIELD_PATH_SCOPE_EVENT_SPECIFIC_CONTEXT: |
8a432889 | 472 | return "EVENT_SPECIFIC_CONTEXT"; |
e7ceb9df | 473 | case BT_FIELD_PATH_SCOPE_EVENT_PAYLOAD: |
8a432889 | 474 | return "EVENT_PAYLOAD"; |
3dca2276 | 475 | } |
00dff0ec SM |
476 | |
477 | bt_common_abort(); | |
3dca2276 PP |
478 | } |
479 | ||
480 | static inline | |
481 | const char *bt_common_event_class_log_level_string( | |
482 | enum bt_event_class_log_level level) | |
483 | { | |
484 | switch (level) { | |
3dca2276 | 485 | case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY: |
8a432889 | 486 | return "EMERGENCY"; |
3dca2276 | 487 | case BT_EVENT_CLASS_LOG_LEVEL_ALERT: |
8a432889 | 488 | return "ALERT"; |
3dca2276 | 489 | case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL: |
8a432889 | 490 | return "CRITICAL"; |
3dca2276 | 491 | case BT_EVENT_CLASS_LOG_LEVEL_ERROR: |
8a432889 | 492 | return "ERROR"; |
3dca2276 | 493 | case BT_EVENT_CLASS_LOG_LEVEL_WARNING: |
8a432889 | 494 | return "WARNING"; |
3dca2276 | 495 | case BT_EVENT_CLASS_LOG_LEVEL_NOTICE: |
8a432889 | 496 | return "NOTICE"; |
3dca2276 | 497 | case BT_EVENT_CLASS_LOG_LEVEL_INFO: |
8a432889 | 498 | return "INFO"; |
3dca2276 | 499 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM: |
8a432889 | 500 | return "DEBUG_SYSTEM"; |
3dca2276 | 501 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM: |
8a432889 | 502 | return "DEBUG_PROGRAM"; |
3dca2276 | 503 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS: |
8a432889 | 504 | return "DEBUG_PROCESS"; |
3dca2276 | 505 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE: |
8a432889 | 506 | return "DEBUG_MODULE"; |
3dca2276 | 507 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT: |
8a432889 | 508 | return "DEBUG_UNIT"; |
3dca2276 | 509 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION: |
8a432889 | 510 | return "DEBUG_FUNCTION"; |
3dca2276 | 511 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE: |
8a432889 | 512 | return "DEBUG_LINE"; |
3dca2276 | 513 | case BT_EVENT_CLASS_LOG_LEVEL_DEBUG: |
8a432889 | 514 | return "DEBUG"; |
3dca2276 | 515 | } |
00dff0ec SM |
516 | |
517 | bt_common_abort(); | |
3dca2276 PP |
518 | }; |
519 | ||
da91b29a PP |
520 | static inline |
521 | const char *bt_common_value_type_string(enum bt_value_type type) | |
522 | { | |
523 | switch (type) { | |
524 | case BT_VALUE_TYPE_NULL: | |
8a432889 | 525 | return "NULL"; |
da91b29a | 526 | case BT_VALUE_TYPE_BOOL: |
8a432889 | 527 | return "BOOL"; |
00dff0ec SM |
528 | case BT_VALUE_TYPE_INTEGER: |
529 | return "INTEGER"; | |
fdd3a2da | 530 | case BT_VALUE_TYPE_UNSIGNED_INTEGER: |
8a432889 | 531 | return "UNSIGNED_INTEGER"; |
fdd3a2da | 532 | case BT_VALUE_TYPE_SIGNED_INTEGER: |
8a432889 | 533 | return "SIGNED_INTEGER"; |
da91b29a | 534 | case BT_VALUE_TYPE_REAL: |
8a432889 | 535 | return "REAL"; |
da91b29a | 536 | case BT_VALUE_TYPE_STRING: |
8a432889 | 537 | return "STRING"; |
da91b29a | 538 | case BT_VALUE_TYPE_ARRAY: |
8a432889 | 539 | return "ARRAY"; |
da91b29a | 540 | case BT_VALUE_TYPE_MAP: |
8a432889 | 541 | return "MAP"; |
da91b29a | 542 | } |
00dff0ec SM |
543 | |
544 | bt_common_abort(); | |
da91b29a PP |
545 | }; |
546 | ||
0ee90b74 PP |
547 | static inline |
548 | const char *bt_common_logging_level_string( | |
549 | enum bt_logging_level level) | |
550 | { | |
551 | switch (level) { | |
ef267d12 | 552 | case BT_LOGGING_LEVEL_TRACE: |
8a432889 | 553 | return "TRACE"; |
0ee90b74 | 554 | case BT_LOGGING_LEVEL_DEBUG: |
8a432889 | 555 | return "DEBUG"; |
0ee90b74 | 556 | case BT_LOGGING_LEVEL_INFO: |
8a432889 | 557 | return "INFO"; |
770538dd | 558 | case BT_LOGGING_LEVEL_WARNING: |
8a432889 | 559 | return "WARNING"; |
0ee90b74 | 560 | case BT_LOGGING_LEVEL_ERROR: |
8a432889 | 561 | return "ERROR"; |
0ee90b74 | 562 | case BT_LOGGING_LEVEL_FATAL: |
8a432889 | 563 | return "FATAL"; |
0ee90b74 | 564 | case BT_LOGGING_LEVEL_NONE: |
8a432889 | 565 | return "NONE"; |
0ee90b74 | 566 | } |
00dff0ec SM |
567 | |
568 | bt_common_abort(); | |
0ee90b74 PP |
569 | }; |
570 | ||
de625d1c | 571 | static inline |
d24d5663 | 572 | const char *bt_common_func_status_string(int status) |
de625d1c PP |
573 | { |
574 | switch (status) { | |
520cdc82 | 575 | case __BT_FUNC_STATUS_OVERFLOW_ERROR: |
d24d5663 | 576 | return "OVERFLOW"; |
d24d5663 PP |
577 | case __BT_FUNC_STATUS_MEMORY_ERROR: |
578 | return "MEMORY_ERROR"; | |
27c61ce8 PP |
579 | case __BT_FUNC_STATUS_USER_ERROR: |
580 | return "USER_ERROR"; | |
d24d5663 PP |
581 | case __BT_FUNC_STATUS_ERROR: |
582 | return "ERROR"; | |
583 | case __BT_FUNC_STATUS_OK: | |
584 | return "OK"; | |
585 | case __BT_FUNC_STATUS_END: | |
586 | return "END"; | |
587 | case __BT_FUNC_STATUS_NOT_FOUND: | |
588 | return "NOT_FOUND"; | |
9b4f9b42 PP |
589 | case __BT_FUNC_STATUS_INTERRUPTED: |
590 | return "INTERRUPTED"; | |
a6804e94 SM |
591 | case __BT_FUNC_STATUS_NO_MATCH: |
592 | return "NO_MATCH"; | |
593 | case __BT_FUNC_STATUS_AGAIN: | |
594 | return "AGAIN"; | |
595 | case __BT_FUNC_STATUS_UNKNOWN_OBJECT: | |
596 | return "UNKNOWN_OBJECT"; | |
de625d1c | 597 | } |
00dff0ec SM |
598 | |
599 | bt_common_abort(); | |
de625d1c PP |
600 | } |
601 | ||
7de0e49a PP |
602 | #define NS_PER_S_I INT64_C(1000000000) |
603 | #define NS_PER_S_U UINT64_C(1000000000) | |
604 | ||
605 | static inline | |
606 | int bt_common_clock_value_from_ns_from_origin( | |
607 | int64_t cc_offset_seconds, uint64_t cc_offset_cycles, | |
608 | uint64_t cc_freq, int64_t ns_from_origin, | |
609 | uint64_t *raw_value) | |
610 | { | |
611 | int ret = 0; | |
612 | int64_t offset_in_ns; | |
613 | uint64_t value_in_ns; | |
614 | uint64_t rem_value_in_ns; | |
615 | uint64_t value_periods; | |
616 | uint64_t value_period_cycles; | |
617 | int64_t ns_to_add; | |
618 | ||
98b15851 | 619 | BT_ASSERT_DBG(raw_value); |
7de0e49a PP |
620 | |
621 | /* Compute offset part of requested value, in nanoseconds */ | |
622 | if (!bt_safe_to_mul_int64(cc_offset_seconds, NS_PER_S_I)) { | |
623 | ret = -1; | |
624 | goto end; | |
625 | } | |
626 | ||
627 | offset_in_ns = cc_offset_seconds * NS_PER_S_I; | |
628 | ||
629 | if (cc_freq == NS_PER_S_U) { | |
630 | ns_to_add = (int64_t) cc_offset_cycles; | |
631 | } else { | |
632 | if (!bt_safe_to_mul_int64((int64_t) cc_offset_cycles, | |
633 | NS_PER_S_I)) { | |
634 | ret = -1; | |
635 | goto end; | |
636 | } | |
637 | ||
638 | ns_to_add = ((int64_t) cc_offset_cycles * NS_PER_S_I) / | |
639 | (int64_t) cc_freq; | |
640 | } | |
641 | ||
642 | if (!bt_safe_to_add_int64(offset_in_ns, ns_to_add)) { | |
643 | ret = -1; | |
644 | goto end; | |
645 | } | |
646 | ||
647 | offset_in_ns += ns_to_add; | |
648 | ||
649 | /* Value part in nanoseconds */ | |
650 | if (ns_from_origin < offset_in_ns) { | |
651 | ret = -1; | |
652 | goto end; | |
653 | } | |
654 | ||
655 | value_in_ns = (uint64_t) (ns_from_origin - offset_in_ns); | |
656 | ||
657 | /* Number of whole clock periods in `value_in_ns` */ | |
658 | value_periods = value_in_ns / NS_PER_S_U; | |
659 | ||
660 | /* Remaining nanoseconds in cycles + whole clock periods in cycles */ | |
661 | rem_value_in_ns = value_in_ns - value_periods * NS_PER_S_U; | |
662 | ||
663 | if (value_periods > UINT64_MAX / cc_freq) { | |
664 | ret = -1; | |
665 | goto end; | |
666 | } | |
667 | ||
668 | if (!bt_safe_to_mul_uint64(value_periods, cc_freq)) { | |
669 | ret = -1; | |
670 | goto end; | |
671 | } | |
672 | ||
673 | value_period_cycles = value_periods * cc_freq; | |
674 | ||
675 | if (!bt_safe_to_mul_uint64(cc_freq, rem_value_in_ns)) { | |
676 | ret = -1; | |
677 | goto end; | |
678 | } | |
679 | ||
680 | if (!bt_safe_to_add_uint64(cc_freq * rem_value_in_ns / NS_PER_S_U, | |
681 | value_period_cycles)) { | |
682 | ret = -1; | |
683 | goto end; | |
684 | } | |
685 | ||
686 | *raw_value = cc_freq * rem_value_in_ns / NS_PER_S_U + | |
687 | value_period_cycles; | |
688 | ||
689 | end: | |
690 | return ret; | |
691 | } | |
692 | ||
5ab3582b MD |
693 | /* |
694 | * bt_g_string_append_printf cannot be inlined because it expects a | |
695 | * variadic argument list. | |
696 | */ | |
1353b066 | 697 | __BT_ATTR_FORMAT_PRINTF(2, 3) |
5ab3582b MD |
698 | int bt_common_g_string_append_printf(GString *str, const char *fmt, ...); |
699 | ||
15b4e74c MD |
700 | static inline |
701 | void bt_common_g_string_append(GString *str, const char *s) | |
702 | { | |
703 | gsize len, allocated_len, s_len; | |
704 | ||
705 | /* str->len excludes \0. */ | |
706 | len = str->len; | |
707 | /* Exclude \0. */ | |
708 | allocated_len = str->allocated_len - 1; | |
709 | s_len = strlen(s); | |
710 | if (G_UNLIKELY(allocated_len < len + s_len)) { | |
711 | /* Resize. */ | |
712 | g_string_set_size(str, len + s_len); | |
713 | } else { | |
714 | str->len = len + s_len; | |
715 | } | |
716 | memcpy(str->str + len, s, s_len + 1); | |
717 | } | |
718 | ||
719 | static inline | |
720 | void bt_common_g_string_append_c(GString *str, char c) | |
721 | { | |
722 | gsize len, allocated_len, s_len; | |
723 | ||
724 | /* str->len excludes \0. */ | |
725 | len = str->len; | |
726 | /* Exclude \0. */ | |
727 | allocated_len = str->allocated_len - 1; | |
728 | s_len = 1; | |
729 | if (G_UNLIKELY(allocated_len < len + s_len)) { | |
730 | /* Resize. */ | |
731 | g_string_set_size(str, len + s_len); | |
732 | } else { | |
733 | str->len = len + s_len; | |
734 | } | |
735 | str->str[len] = c; | |
736 | str->str[len + 1] = '\0'; | |
737 | } | |
738 | ||
6375b942 SM |
739 | static inline |
740 | const char *bt_common_component_class_type_string( | |
741 | enum bt_component_class_type type) | |
742 | { | |
743 | switch (type) { | |
744 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
745 | return "SOURCE"; | |
746 | case BT_COMPONENT_CLASS_TYPE_SINK: | |
747 | return "SINK"; | |
748 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
749 | return "FILTER"; | |
6375b942 | 750 | } |
00dff0ec SM |
751 | |
752 | bt_common_abort(); | |
6375b942 SM |
753 | } |
754 | ||
1e123ed6 SM |
755 | static inline |
756 | const char *bt_common_message_type_string(enum bt_message_type type) | |
757 | { | |
758 | switch (type) { | |
759 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: | |
760 | return "STREAM_BEGINNING"; | |
761 | case BT_MESSAGE_TYPE_STREAM_END: | |
762 | return "STREAM_END"; | |
763 | case BT_MESSAGE_TYPE_EVENT: | |
764 | return "EVENT"; | |
765 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
766 | return "PACKET_BEGINNING"; | |
767 | case BT_MESSAGE_TYPE_PACKET_END: | |
768 | return "PACKET_END"; | |
769 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
770 | return "DISCARDED_EVENTS"; | |
771 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
772 | return "DISCARDED_PACKETS"; | |
773 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: | |
774 | return "MESSAGE_ITERATOR_INACTIVITY"; | |
775 | } | |
776 | ||
777 | bt_common_abort(); | |
778 | } | |
779 | ||
97ebbaa0 PP |
780 | #ifdef __cplusplus |
781 | } | |
782 | #endif | |
783 | ||
1670bffd | 784 | #endif /* BABELTRACE_COMMON_INTERNAL_H */ |